Skip to content

Commit

Permalink
Removing limitations on "using" directives.
Browse files Browse the repository at this point in the history
  • Loading branch information
LurkingNinjaDev committed Nov 21, 2023
1 parent 528c4c3 commit 059ec8c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.

[READ ME](./README.md) | [MIT LICENSE](./LICENSE)
## [0.0.3] - 2022-11-20
### Changed
- Limitation of missing using directives was removed.

## [0.0.2] - 2022-11-06
### Added
- This CHANGELOG
Expand Down
10 changes: 5 additions & 5 deletions Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ internal static class Common
* {0} name space if exists
* {1} closing bracket for namespace if needed
* {2} class definition
* {3} using directives
*/
private const string NS_TEMPLATE = @"using System;
using UnityEngine;
private const string NS_TEMPLATE = @"{3}
{0}
{2}
{1}";
Expand Down Expand Up @@ -50,13 +49,14 @@ static void ApplyStaticFieldsAndEventHandlers()
}}
}}";

internal static string NamespaceTemplateResolve(string nameSpace, string source)
internal static string NamespaceTemplateResolve(string usingDirectives, string nameSpace, string source)
{
var ns = GetNamespaceTemplate(nameSpace);
return string.Format(NS_TEMPLATE,
/*{0}*/ns.Item1,
/*{1}*/ns.Item2,
/*{2}*/source);
/*{2}*/source,
/*{3}*/usingDirectives);
}

internal static bool IsPartial(ClassDeclarationSyntax cds) =>
Expand Down
4 changes: 2 additions & 2 deletions DomainReloadSG.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>LurkingNinja.DomainReloadSG</RootNamespace>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
<AssemblyVersion>0.2</AssemblyVersion>
<FileVersion>0.2</FileVersion>
<AssemblyVersion>0.3</AssemblyVersion>
<FileVersion>0.3</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ You have two ways to use this repository:
### Limitations
- The class you're augmenting _has to be_ decorated with the ```partial``` keyword.
- Currently there is no way of excluding static fields or methods from this service.
- If you're using extra namespaces currently you need to explicitly write it in the code as opposed of in a ```using``` statement.

### Exclusion
- Decorate your partial class with ```[NoDomainReloadSupport]``` attribute if you want to exclude it completely.

### Example
```csharp
using LurkingNinja.FirstNameSpace.SecondNameSpace.ForTest;
using UnityEngine;

public partial class TestStaticOnNoDomainReload : MonoBehaviour
Expand All @@ -44,6 +44,7 @@ public partial class TestStaticOnNoDomainReload : MonoBehaviour
Debug.Log($"Started with {_number}");
_number += 10;
Application.quitting += OnQuit;
OtherTestEvent.OnChangeSomethingStatic += OnQuit;
Debug.Log($"Ended with {_number}");
}

Expand All @@ -59,17 +60,17 @@ Obviously the ```Edit > Project Settings > Editor > Enter Play Mode``` is set an
The generated source code:
```TestStaticOnNoDomainReload_codegen.cs```
```csharp
using System;
using LurkingNinja.FirstNameSpace.SecondNameSpace.ForTest;
using UnityEngine;

public partial class TestStaticOnNoDomainReload
{
[UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.SubsystemRegistration)]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ApplyStaticFieldsAndEventHandlers()
{
_number = default;
Application.quitting -= OnQuit;

OtherTestEvent.OnChangeSomethingStatic -= OnQuit;
}
}
```
11 changes: 9 additions & 2 deletions SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ public void Execute(GeneratorExecutionContext context)

var nameSpace = Common.GetNamespace(dsr.ClassToAugment);

var lines = new StringBuilder();
var usingDirectives = new StringBuilder();
foreach (var child in dsr.ClassToAugment.SyntaxTree.GetRoot().ChildNodes())
if (child.IsKind(SyntaxKind.UsingDirective))
usingDirectives.AppendLine(child.ToString());
usingDirectives.AppendLine("using System;");
usingDirectives.AppendLine("using UnityEngine;");

var lines = new StringBuilder();

foreach (var field in dsr.fields)
{
if (field.Declaration.Variables.Count < 1) continue;
Expand All @@ -46,7 +53,7 @@ public void Execute(GeneratorExecutionContext context)
/*{1}*/lines);

Common.AddSource(context, dsr.ClassToAugment.Identifier.ToFullString().Trim(),
Common.NamespaceTemplateResolve(nameSpace, source));
Common.NamespaceTemplateResolve(usingDirectives.ToString(), nameSpace, source));
}
}

Expand Down

0 comments on commit 059ec8c

Please sign in to comment.