forked from leeveel/GeekServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
116 changed files
with
2,301 additions
and
544 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,3 +354,4 @@ MigrationBackup/ | |
/UnityDemo/Library | ||
/UnityDemo/Temp | ||
/.idea | ||
/database |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
global using MongoDB.Bson.Serialization.Attributes; | ||
global using MongoDB.Bson.Serialization.Options; | ||
global using Geek.Server.Proto; | ||
global using MongoDB.Bson.Serialization.Options; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Geek.Server.CodeGenerator.State | ||
{ | ||
public class DBStateFilter : ISyntaxReceiver | ||
{ | ||
public const string NESTED_CLASS_DELIMITER = "."; | ||
public const string NAMESPACE_CLASS_DELIMITER = "."; | ||
|
||
public Dictionary<string, string> classFullNamePair = new Dictionary<string, string>(); | ||
public Dictionary<string, List<string>> classPair = new Dictionary<string, List<string>>(); | ||
|
||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
{ | ||
if (syntaxNode is ClassDeclarationSyntax source) | ||
{ | ||
if (source.BaseList == null) | ||
return; | ||
var baseTypes = source.BaseList.Types.ToArray(); | ||
var list = new List<string>(); | ||
|
||
var className = source.Identifier.Text; | ||
classFullNamePair[className] = GetFullName(source); | ||
classPair[source.Identifier.Text] = list; | ||
foreach (var v in baseTypes) | ||
{ | ||
list.Add(v.ToString()); | ||
} | ||
} | ||
} | ||
|
||
public static string GetFullName(ClassDeclarationSyntax source) | ||
{ | ||
var items = new List<string>(); | ||
var parent = source.Parent; | ||
while (parent.IsKind(SyntaxKind.ClassDeclaration)) | ||
{ | ||
var parentClass = parent as ClassDeclarationSyntax; | ||
if (parentClass == null) | ||
{ | ||
break; | ||
} | ||
items.Add(parentClass.Identifier.Text); | ||
|
||
parent = parent.Parent; | ||
} | ||
|
||
var nameSpace = parent as NamespaceDeclarationSyntax; | ||
|
||
var sb = new StringBuilder(); | ||
if (nameSpace != null) | ||
sb.Append(nameSpace.Name); | ||
sb.Append(NAMESPACE_CLASS_DELIMITER); | ||
items.Reverse(); | ||
items.ForEach(i => { sb.Append(i).Append(NESTED_CLASS_DELIMITER); }); | ||
sb.Append(source.Identifier.Text); | ||
|
||
var result = sb.ToString(); | ||
return result; | ||
} | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
Geek.Server.CodeGenerator/State/MessagePackRegisterGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
using System.Collections.Generic; | ||
using Geek.Server.CodeGenerator.Utils; | ||
using Microsoft.CodeAnalysis; | ||
using Scriban; | ||
|
||
namespace Geek.Server.CodeGenerator.State | ||
{ | ||
[Generator] | ||
public class MessagePackRegisterGenerator : ISourceGenerator | ||
{ | ||
const string baseDBStateName = "InnerState"; | ||
const string dBStateName = "CacheState"; | ||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
//Debugger.Launch(); | ||
ResLoader.LoadDll(); | ||
context.RegisterForSyntaxNotifications(() => new DBStateFilter()); | ||
} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
if (context.SyntaxReceiver is DBStateFilter receiver) | ||
{ | ||
var str = ResLoader.LoadTemplate("PolymorphicDBStateRegister.liquid"); | ||
Template agentTemplate = Template.Parse(str); | ||
|
||
var inheritDBBaseMap = new Dictionary<string, string>(); | ||
foreach (var v in receiver.classFullNamePair) | ||
{ | ||
var c = GetInheritDBBase(receiver.classPair, v.Key, v.Key); | ||
if (c != "") | ||
{ | ||
inheritDBBaseMap[v.Key] = c; | ||
} | ||
} | ||
|
||
var assemblyName = context.Compilation.AssemblyName; | ||
if (assemblyName == null) | ||
assemblyName = ""; | ||
var arr = new PolymorphicInfoArray(); | ||
arr.prefix = assemblyName.Replace(".", ""); | ||
|
||
foreach (var v in inheritDBBaseMap) | ||
{ | ||
var keyFullName = GetFullName(receiver.classFullNamePair, v.Key); | ||
AddInfo(arr, keyFullName, keyFullName); | ||
AddGetAllSubClass(arr, receiver.classFullNamePair, inheritDBBaseMap, v.Key, v.Key); | ||
} | ||
|
||
RemoveFinalClass(arr, inheritDBBaseMap, receiver.classFullNamePair); | ||
|
||
var source = agentTemplate.Render(arr); | ||
//File.WriteAllText($"PolymorphicDBStateRegister.cs", source); | ||
context.AddSource($"{arr.prefix}PolymorphicDBStateRegister.g.cs", source); | ||
} | ||
} | ||
|
||
string GetInheritDBBase(Dictionary<string, List<string>> classPair, string startClassName, string currentClassName) | ||
{ | ||
if (classPair.TryGetValue(currentClassName, out var list)) | ||
{ | ||
foreach (var l in list) | ||
{ | ||
if (l == baseDBStateName) | ||
{ | ||
return currentClassName == startClassName ? baseDBStateName : currentClassName; | ||
} | ||
if (l == dBStateName) | ||
{ | ||
return currentClassName == startClassName ? dBStateName : currentClassName; | ||
} | ||
var str = GetInheritDBBase(classPair, startClassName, l); | ||
if (str != "") | ||
return str; | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
string GetFullName(Dictionary<string, string> nameMap, string name) | ||
{ | ||
if (nameMap.TryGetValue(name, out var outName)) | ||
{ | ||
return outName; | ||
} | ||
if (name == baseDBStateName) | ||
{ | ||
return "Geek.Server.Core.Storage.InnerState"; | ||
} | ||
if (name == dBStateName) | ||
{ | ||
return "Geek.Server.Core.Storage.CacheState"; | ||
} | ||
return ""; | ||
} | ||
|
||
void AddGetAllSubClass(PolymorphicInfoArray arr, Dictionary<string, string> fullNameMap, Dictionary<string, string> inheritDBBaseMap, string startClass, string curClass) | ||
{ | ||
var baseName = GetFullName(fullNameMap, startClass); | ||
while (true) | ||
{ | ||
if (inheritDBBaseMap.TryGetValue(curClass, out var inher)) | ||
{ | ||
var subname = GetFullName(fullNameMap, inher); | ||
if (arr.infos.Find(v => v.basename == baseName && v.subname == subname) != null) | ||
{ | ||
return; | ||
} | ||
AddInfo(arr, baseName, subname); | ||
if (inher == baseDBStateName || inher == dBStateName) | ||
{ | ||
return; | ||
} | ||
AddGetAllSubClass(arr, fullNameMap, inheritDBBaseMap, startClass, inher); | ||
} | ||
} | ||
} | ||
|
||
void AddInfo(PolymorphicInfoArray arr, string baseName, string subName) | ||
{ | ||
//if (baseName != subName) | ||
arr.infos.Add(new PolymorphicInfo { basename = baseName, subname = subName, subsid = ((int)MurmurHash3.Hash(baseName, 27)).ToString() }); | ||
//arr.infos.Add(new PolymorphicInfo { basename = baseName, subname = subName, subsid = ((int)xxHash32.ComputeHash(nameBytes, 0, nameBytes.Length, 27)).ToString() }); | ||
} | ||
|
||
void RemoveFinalClass(PolymorphicInfoArray arr, Dictionary<string, string> inheritDBBaseMap, Dictionary<string, string> classFullNamePair) | ||
{ | ||
var all = arr.infos.ToArray(); | ||
foreach (var c in all) | ||
{ | ||
if (c.basename == c.subname) | ||
{ | ||
bool needRemove = true; | ||
foreach (var kv in inheritDBBaseMap) | ||
{ | ||
if (c.basename == GetFullName(classFullNamePair, kv.Value)) | ||
{ | ||
needRemove = false; | ||
break; | ||
} | ||
} | ||
if (needRemove) | ||
{ | ||
arr.infos.Remove(c); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.