Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public sealed class Definition
/// </summary>
public static readonly List<string> CheckMethodNameList =
[
"AddUIEvent", "AddEventListener"
"AddUIEvent", "AddEventListener", "RemoveEventListener"
];

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
[Generator]
public class EventInterfaceGenerator : ISourceGenerator
{
private sealed class EventCenterInterfaceInfo
private sealed class GameEventHelperInterfaceInfo
{
public string InterfaceName { get; set; } = string.Empty;
public string EventClassName { get; set; } = string.Empty;
public string GroupClassName { get; set; } = string.Empty;
public List<EventCenterMethodInfo> Methods { get; } = new List<EventCenterMethodInfo>();
public List<GameEventHelperMethodInfo> Methods { get; } = new List<GameEventHelperMethodInfo>();
}

private sealed class EventCenterMethodInfo
private sealed class GameEventHelperMethodInfo
{
public string MethodName { get; set; } = string.Empty;
public string ActionType { get; set; } = string.Empty;
Expand All @@ -31,7 +31,7 @@ public void Execute(GeneratorExecutionContext context)
{
var syntaxTrees = context.Compilation.SyntaxTrees;
List<string> classNameList = new List<string>();
List<EventCenterInterfaceInfo> eventCenterInterfaceInfos = new List<EventCenterInterfaceInfo>();
List<GameEventHelperInterfaceInfo> gameEventHelperInterfaceInfos = new List<GameEventHelperInterfaceInfo>();

foreach (var tree in syntaxTrees)
{
Expand Down Expand Up @@ -61,15 +61,15 @@ public void Execute(GeneratorExecutionContext context)
context.AddSource($"{interfaceName}_Gen.g.cs", implementationClassCode);

classNameList.Add($"{interfaceName}_Gen");
eventCenterInterfaceInfos.Add(GenerateEventCenterInterfaceInfo(interfaceName, eventClassName, interfaceNode, context));
gameEventHelperInterfaceInfos.Add(GenerateGameEventHelperInterfaceInfo(interfaceName, eventClassName, interfaceNode, context));
}
}

if (classNameList.Count > 0)
{
string uniqueFileName = $"GameEventHelper.g.cs";
string uniqueFileName = $"GameEventHelper.Init.g.cs";
context.AddSource(uniqueFileName, GenerateGameEventHelper(classNameList));
context.AddSource("EventCenter.g.cs", GenerateEventCenter(eventCenterInterfaceInfos));
context.AddSource("GameEventHelper.g.cs", GenerateGameEventHelper(gameEventHelperInterfaceInfos));
}
}

Expand All @@ -90,7 +90,7 @@ private string GenerateGameEventHelper(List<string> classNameList)
sb.AppendLine();
sb.AppendLine($"namespace {Definition.NameSpace}");
sb.AppendLine($"{{");
sb.AppendLine($" public static class GameEventHelper");
sb.AppendLine($" public static partial class GameEventHelper");
sb.AppendLine(" {");
sb.AppendLine($" public static void Init()");
sb.AppendLine(" {");
Expand All @@ -106,7 +106,7 @@ private string GenerateGameEventHelper(List<string> classNameList)
return sb.ToString();
}

private string GenerateEventCenter(List<EventCenterInterfaceInfo> eventCenterInterfaceInfos)
private string GenerateGameEventHelper(List<GameEventHelperInterfaceInfo> gameEventHelperInterfaceInfos)
{
var sb = new StringBuilder();
sb.AppendLine($"//------------------------------------------------------------------------------");
Expand All @@ -123,24 +123,24 @@ private string GenerateEventCenter(List<EventCenterInterfaceInfo> eventCenterInt
sb.AppendLine();
sb.AppendLine($"namespace {Definition.NameSpace}");
sb.AppendLine($"{{");
sb.AppendLine($" public partial class EventCenter");
sb.AppendLine($" public static partial class GameEventHelper");
sb.AppendLine(" {");
GenerateEventCenterGroup(sb, eventCenterInterfaceInfos, "AddEvent", "AddEventListener");
GenerateGameEventHelperGroup(sb, gameEventHelperInterfaceInfos, "AddEvent", "AddEventListener");
sb.AppendLine();
GenerateEventCenterGroup(sb, eventCenterInterfaceInfos, "RemoveEvent", "RemoveEventListener");
GenerateGameEventHelperGroup(sb, gameEventHelperInterfaceInfos, "RemoveEvent", "RemoveEventListener");
sb.AppendLine(" }");
sb.AppendLine("}");
return sb.ToString();
}

private void GenerateEventCenterGroup(StringBuilder sb, List<EventCenterInterfaceInfo> eventCenterInterfaceInfos, string className, string methodName)
private void GenerateGameEventHelperGroup(StringBuilder sb, List<GameEventHelperInterfaceInfo> gameEventHelperInterfaceInfos, string className, string methodName)
{
sb.AppendLine($" public partial class {className}");
sb.AppendLine(" {");

for (int i = 0; i < eventCenterInterfaceInfos.Count; i++)
for (int i = 0; i < gameEventHelperInterfaceInfos.Count; i++)
{
var interfaceInfo = eventCenterInterfaceInfos[i];
var interfaceInfo = gameEventHelperInterfaceInfos[i];
sb.AppendLine($" public partial class {interfaceInfo.GroupClassName}");
sb.AppendLine(" {");

Expand All @@ -160,7 +160,7 @@ private void GenerateEventCenterGroup(StringBuilder sb, List<EventCenterInterfac

sb.AppendLine(" }");

if (i < eventCenterInterfaceInfos.Count - 1)
if (i < gameEventHelperInterfaceInfos.Count - 1)
{
sb.AppendLine();
}
Expand All @@ -169,29 +169,29 @@ private void GenerateEventCenterGroup(StringBuilder sb, List<EventCenterInterfac
sb.AppendLine(" }");
}

private EventCenterInterfaceInfo GenerateEventCenterInterfaceInfo(string interfaceName, string eventClassName, InterfaceDeclarationSyntax interfaceNode, GeneratorExecutionContext context)
private GameEventHelperInterfaceInfo GenerateGameEventHelperInterfaceInfo(string interfaceName, string eventClassName, InterfaceDeclarationSyntax interfaceNode, GeneratorExecutionContext context)
{
var semanticModel = context.Compilation.GetSemanticModel(interfaceNode.SyntaxTree);
var eventCenterInterfaceInfo = new EventCenterInterfaceInfo
var gameEventHelperInterfaceInfo = new GameEventHelperInterfaceInfo
{
InterfaceName = interfaceName,
EventClassName = eventClassName,
GroupClassName = GetEventCenterGroupName(interfaceName),
GroupClassName = GetGameEventHelperGroupName(interfaceName),
};

foreach (var method in interfaceNode.Members.OfType<MethodDeclarationSyntax>())
{
eventCenterInterfaceInfo.Methods.Add(new EventCenterMethodInfo
gameEventHelperInterfaceInfo.Methods.Add(new GameEventHelperMethodInfo
{
MethodName = method.Identifier.ToString(),
ActionType = GenerateActionType(method, semanticModel),
});
}

return eventCenterInterfaceInfo;
return gameEventHelperInterfaceInfo;
}

private string GetEventCenterGroupName(string interfaceName)
private string GetGameEventHelperGroupName(string interfaceName)
{
if (interfaceName.Length > 1 && interfaceName[0] == 'I' && char.IsUpper(interfaceName[1]))
{
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static class SetSpriteExtensions
/// <param name="cancellationToken">取消设置资源的Token。</param>
public static void SetSprite(this Image image, string location, bool setNativeSize = false, Action<Image> callback = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(location))
{
return;
}
ResourceExtComponent.Instance.SetAssetByResources<Sprite>(SetSpriteObject.Create(image, location, setNativeSize, callback, cancellationToken), cancellationToken).Forget();
}

Expand All @@ -29,6 +33,10 @@ public static void SetSprite(this Image image, string location, bool setNativeSi
/// <param name="cancellationToken">取消设置资源的Token。</param>
public static void SetSprite(this SpriteRenderer spriteRenderer, string location, Action<SpriteRenderer> callback = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(location))
{
return;
}
ResourceExtComponent.Instance.SetAssetByResources<Sprite>(SetSpriteObject.Create(spriteRenderer, location, callback, cancellationToken), cancellationToken).Forget();
}

Expand All @@ -42,6 +50,10 @@ public static void SetSprite(this SpriteRenderer spriteRenderer, string location
/// <param name="cancellationToken">取消设置资源的Token。</param>
public static void SetSubSprite(this Image image, string location, string spriteName, bool setNativeSize = false, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(location) || string.IsNullOrEmpty(spriteName))
{
return;
}
ResourceExtComponent.Instance.SetSubSprite(image, location, spriteName, setNativeSize, cancellationToken).Forget();
}

Expand All @@ -54,6 +66,10 @@ public static void SetSubSprite(this Image image, string location, string sprite
/// <param name="cancellationToken">取消设置资源的Token。</param>
public static void SetSubSprite(this SpriteRenderer spriteRenderer, string location, string spriteName, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(location) || string.IsNullOrEmpty(spriteName))
{
return;
}
ResourceExtComponent.Instance.SetSubSprite(spriteRenderer, location, spriteName, cancellationToken).Forget();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public async UniTaskVoid SetAssetByResources<T>(ISetAssetObject setAssetObject,
loadedResource = await _resourceModule.LoadAssetAsync<T>(location, linkedTokenSource.Token);
if (loadedResource == null)
{
if (linkedTokenSource.IsCancellationRequested)
{
return;
}
Log.Error("加载资源失败,资源为空: '{0}'", location);
return;
}
Expand Down