Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed Dec 12, 2019
2 parents 38e8177 + f352a6f commit 2c19e10
Show file tree
Hide file tree
Showing 11 changed files with 725 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[duktape](https://github.com/svaarala/duktape)集成到Unity中, 使您可以在运行时动态加载执行javascript脚本. <br/>
另外也提供了对typescript的集成支持, 建议使用typescript进行脚本编写.

技术交流QQ群: 859823032

![editing script](res/ts_editing_1.png "so brilliant!")

# 特点
Expand Down
3 changes: 3 additions & 0 deletions unity/Assets/Duktape/Editor/CodeGenHelper_Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ public override void Dispose()
tsPropertyPrefix += "readonly ";
}
var tsPropertyType = this.cg.bindingManager.GetTSTypeFullName(bindingInfo.propertyInfo.PropertyType);
cg.AppendJSDoc(bindingInfo.propertyInfo);
cg.tsDeclare.AppendLine($"{tsPropertyPrefix}{tsPropertyVar}: {tsPropertyType}");
}
if (bindingInfo.instancePair.IsValid())
Expand All @@ -348,6 +349,7 @@ public override void Dispose()
tsPropertyPrefix += "readonly ";
}
var tsPropertyType = this.cg.bindingManager.GetTSTypeFullName(bindingInfo.propertyInfo.PropertyType);
cg.AppendJSDoc(bindingInfo.propertyInfo);
cg.tsDeclare.AppendLine($"{tsPropertyPrefix}{tsPropertyVar}: {tsPropertyType}");
}
}
Expand Down Expand Up @@ -375,6 +377,7 @@ public override void Dispose()
tsFieldPrefix += "readonly ";
}
var tsFieldType = this.cg.bindingManager.GetTSTypeFullName(bindingInfo.fieldInfo.FieldType);
cg.AppendJSDoc(bindingInfo.fieldInfo);
cg.tsDeclare.AppendLine($"{tsFieldPrefix}{tsFieldVar}: {tsFieldType}");
}
foreach (var kv in bindingInfo.events)
Expand Down
10 changes: 6 additions & 4 deletions unity/Assets/Duktape/Editor/CodeGenHelper_Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ public override void Dispose()
this.cg.cs.AppendLine("duk_begin_enum(ctx, \"{0}\", typeof({1}));",
bindingInfo.jsName,
this.cg.bindingManager.GetCSTypeFullName(bindingInfo.type));
var values = new Dictionary<string, int>();
var values = new Dictionary<string, object>();
foreach (var ev in Enum.GetValues(bindingInfo.type))
{
values[Enum.GetName(bindingInfo.type, ev)] = Convert.ToInt32(ev);
values[Enum.GetName(bindingInfo.type, ev)] = ev;
}
foreach (var kv in values)
{
var name = kv.Key;
var value = kv.Value;
this.cg.cs.AppendLine($"duk_add_const(ctx, \"{name}\", {value}, {-2});");
this.cg.tsDeclare.AppendLine($"{name} = {value},");
var pvalue = Convert.ToInt32(value);
this.cg.cs.AppendLine($"duk_add_const(ctx, \"{name}\", {pvalue}, {-2});");
this.cg.AppendEnumJSDoc(bindingInfo.type, value);
this.cg.tsDeclare.AppendLine($"{name} = {pvalue},");
}
this.cg.cs.AppendLine("duk_end_enum(ctx);");
}
Expand Down
112 changes: 110 additions & 2 deletions unity/Assets/Duktape/Editor/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,123 @@ public string AppendGetArgCount(bool isVararg)
return null;
}

public void AppendJSDoc(MemberInfo info)
public void AppendJSDoc(Type type)
{
var jsdoc = info.GetCustomAttribute(typeof(JSDocAttribute), false) as JSDocAttribute;
if (bindingManager.prefs.doc)
{
var doc = DocResolver.GetDocBody(type);
if (doc != null)
{
AppendJSDoc(doc);
return;
}
}
var jsdoc = type.GetCustomAttribute(typeof(JSDocAttribute), false) as JSDocAttribute;
if (jsdoc != null)
{
AppendJSDoc(jsdoc.lines);
}
}

public void AppendJSDoc(PropertyInfo propertyInfo)
{
if (bindingManager.prefs.doc)
{
var doc = DocResolver.GetDocBody(propertyInfo);
if (doc != null)
{
AppendJSDoc(doc);
return;
}
}
var jsdoc = propertyInfo.GetCustomAttribute(typeof(JSDocAttribute), false) as JSDocAttribute;
if (jsdoc != null)
{
AppendJSDoc(jsdoc.lines);
}
}

public void AppendJSDoc(FieldInfo fieldInfo)
{
if (bindingManager.prefs.doc)
{
var doc = DocResolver.GetDocBody(fieldInfo);
if (doc != null)
{
AppendJSDoc(doc);
return;
}
}
var jsdoc = fieldInfo.GetCustomAttribute(typeof(JSDocAttribute), false) as JSDocAttribute;
if (jsdoc != null)
{
AppendJSDoc(jsdoc.lines);
}
}

public void AppendEnumJSDoc(Type type, object value)
{
if (bindingManager.prefs.doc)
{
var resolver = DocResolver.GetResolver(type.Assembly);
var doc = resolver.GetFieldDocBody(type.FullName + "." + Enum.GetName(type, value));
if (doc != null)
{
AppendJSDoc(doc);
return;
}
}
}

public void AppendJSDoc<T>(T methodInfo)
where T : MethodBase
{
if (bindingManager.prefs.doc)
{
var doc = DocResolver.GetDocBody(methodInfo);
if (doc != null)
{
AppendJSDoc(doc);
return;
}
}
var jsdoc = methodInfo.GetCustomAttribute(typeof(JSDocAttribute), false) as JSDocAttribute;
if (jsdoc != null)
{
AppendJSDoc(jsdoc.lines);
}
}

public void AppendJSDoc(DocResolver.DocBody body)
{
if (body.summary.Length > 1)
{
this.tsDeclare.AppendLine("/**");
foreach (var line in body.summary)
{
this.tsDeclare.AppendLine(" * {0}", line.Replace('\r', ' '));
}
}
else
{
this.tsDeclare.AppendLine("/** {0}", body.summary[0]);
}
foreach (var kv in body.parameters)
{
var pname = kv.Key;
var ptext = kv.Value;
if (!string.IsNullOrEmpty(ptext))
{
this.tsDeclare.AppendLine($" * @param {pname} {ptext}");
}
}
if (!string.IsNullOrEmpty(body.returns))
{
this.tsDeclare.AppendLine($" * @returns {body.returns}");
}
this.tsDeclare.AppendLine(" */");
}

public void AppendJSDoc(string[] lines)
{
if (lines != null && lines.Length > 0)
Expand Down
Loading

0 comments on commit 2c19e10

Please sign in to comment.