Skip to content

Commit

Permalink
[unity]解决子类重载基类方法时index.d.ts报错 (#108)
Browse files Browse the repository at this point in the history
* [unity]解决index.d.ts重载父类方法报错

* Add files via upload

* Update Generator.cs
  • Loading branch information
throw-out authored Dec 15, 2020
1 parent e958a58 commit 89fdc60
Showing 1 changed file with 144 additions and 3 deletions.
147 changes: 144 additions & 3 deletions unity/Assets/Puerts/Src/Editor/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,23 @@ public class TsParameterGenInfo
public string TypeName;
public bool IsParams;
public bool IsOptional;

public override bool Equals(object obj)
{
if (obj != null && obj is TsParameterGenInfo)
{
var info = (TsParameterGenInfo)obj;
return this.Name == info.Name && this.TypeName == info.TypeName && this.IsByRef != info.IsByRef && this.IsParams != info.IsParams && this.IsOptional != info.IsOptional;
}
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return base.ToString();
}
}

// #lizard forgives
Expand Down Expand Up @@ -478,6 +494,30 @@ public class TsMethodGenInfo
public string TypeName;
public bool IsConstructor;
public bool IsStatic;
public override bool Equals(object obj)
{
if (obj != null && obj is TsMethodGenInfo)
{
var info = (TsMethodGenInfo)obj;
if (this.ParameterInfos.Length != info.ParameterInfos.Length || this.Name != info.Name || this.TypeName != info.TypeName || this.IsConstructor != info.IsConstructor || this.IsStatic != info.IsStatic)
return false;
for (int i = 0; i < this.ParameterInfos.Length; i++)
{
if (!this.ParameterInfos[i].Equals(info.ParameterInfos[i]))
return false;
}
return true;
}
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return base.ToString();
}
}

public class TsPropertyGenInfo
Expand Down Expand Up @@ -521,6 +561,7 @@ public class TsTypeGenInfo
public bool IsEnum;
public string EnumKeyValues;
public TsMethodGenInfo[] ExtensionMethods;
public bool IsCheckOk = false;
}

public static bool IsGetterOrSetter(MethodInfo method)
Expand Down Expand Up @@ -747,6 +788,92 @@ public class TypingGenInfo
public string TaskDef;
}

static TsMethodGenInfo[] GetMethodGenInfos(Dictionary<string, TsTypeGenInfo> tsGenTypeInfos, TsTypeGenInfo info, bool getBaseMethods)
{
var result = new List<TsMethodGenInfo>();
if (info.Methods != null) result.AddRange(info.Methods);
if (info.ExtensionMethods != null)
{
foreach (var m in info.ExtensionMethods)
{
result.Add(new TsMethodGenInfo()
{
Name = m.Name,
Document = m.Document,
ParameterInfos = m.ParameterInfos,
IsConstructor = m.IsConstructor,
IsStatic = false,
});
}
}
if (getBaseMethods && info.BaseType != null)
{
var baseInfo = info.BaseType;
var baseName = (!string.IsNullOrEmpty(baseInfo.Namespace) ? (baseInfo.Namespace + ".") : "") + baseInfo.Name;
if (tsGenTypeInfos.TryGetValue(baseName, out baseInfo))
{
foreach (var m in GetMethodGenInfos(tsGenTypeInfos, baseInfo, true))
{
if (!result.Contains(m)) result.Add(m);
}
}
}
return result.ToArray();
}
static Dictionary<string, List<TsMethodGenInfo>> SelectMethodGenInfos(Dictionary<string, TsTypeGenInfo> tsGenTypeInfos, TsTypeGenInfo info, bool getBaseMethods)
{
var result = new Dictionary<string, List<TsMethodGenInfo>>();
foreach (var m in GetMethodGenInfos(tsGenTypeInfos, info, getBaseMethods))
{
if (!result.ContainsKey(m.Name))
result.Add(m.Name, new List<TsMethodGenInfo>());
result[m.Name].Add(m);
}
return result;
}
static void CheckMethodGenInfos(Dictionary<string, TsTypeGenInfo> tsGenTypeInfos, TsTypeGenInfo info)
{
if (info.IsCheckOk || info.BaseType == null)
return;

var baseInfo = info.BaseType;
var baseName = (!string.IsNullOrEmpty(baseInfo.Namespace) ? (baseInfo.Namespace + ".") : "") + baseInfo.Name;
if (tsGenTypeInfos.TryGetValue(baseName, out baseInfo) && info.Methods != null && baseInfo.Methods != null)
{
CheckMethodGenInfos(tsGenTypeInfos, baseInfo);

var methods1 = SelectMethodGenInfos(tsGenTypeInfos, baseInfo, true);
var methods2 = SelectMethodGenInfos(tsGenTypeInfos, info, false);

var select = new List<TsMethodGenInfo>(info.Methods);
foreach (var pair in methods1)
{
var name = pair.Key;
if (!methods2.ContainsKey(name))
continue;
var ms1 = pair.Value;
var ms2 = methods2[name];
if (ms2.Count == 0)
continue;
var diffms = new List<TsMethodGenInfo>();
foreach (var m1 in ms1)
{
var diff = true;
foreach (var m2 in ms2)
{
if (m1.Equals(m2)) { diff = false; break; }
}
if (diff) diffms.Add(m1);
}
if (ms2.Count + diffms.Count != ms1.Count)
{
select.AddRange(diffms);
}
}
info.Methods = select.ToArray();
}
info.IsCheckOk = true;
}
static TypingGenInfo ToTypingGenInfo(IEnumerable<Type> types)
{
HashSet<Type> genTypeSet = new HashSet<Type>();
Expand Down Expand Up @@ -782,9 +909,23 @@ static TypingGenInfo ToTypingGenInfo(IEnumerable<Type> types)

if (!genTypeSet.Contains(typeof(Array)) && !refTypes.Contains(typeof(Array))) AddRefType(refTypes, typeof(Array));

var tsTypeGenInfos = new Dictionary<string, TsTypeGenInfo>();
foreach (var t in refTypes.Distinct())
{
var info = ToTsTypeGenInfo(t, genTypeSet);
var name = (string.IsNullOrEmpty(info.Namespace) ? "" : (info.Namespace + ".")) + info.Name;
if (info.IsGenericTypeDefinition)
name += "<" + string.Join(",", info.GenericParameters) + ">";
tsTypeGenInfos.Add(name, info);
}
foreach (var info in tsTypeGenInfos)
{
CheckMethodGenInfos(tsTypeGenInfos, info.Value);
}

return new TypingGenInfo()
{
NamespaceInfos = refTypes.Distinct().Select(t => ToTsTypeGenInfo(t, genTypeSet)).GroupBy(t => t.Namespace)
NamespaceInfos = tsTypeGenInfos.Values.GroupBy(t => t.Namespace)
.Select(g => new TsNamespaceGenInfo()
{
Name = g.Key,
Expand Down Expand Up @@ -950,4 +1091,4 @@ public static void GenerateCode(string saveTo, bool tsOnly = false)
}
}
}
}
}

0 comments on commit 89fdc60

Please sign in to comment.