Skip to content

Commit 3bfeec1

Browse files
author
András Kurai
committed
fix non-alphanumeric characters in filenames
1 parent 17a0b4b commit 3bfeec1

File tree

9 files changed

+67
-4
lines changed

9 files changed

+67
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.3.0
2+
- Support for files with non-alphanumeric characters
3+
- Support for file starting with numeric characters
4+
- Update documentation with name generation rules
5+
16
# 0.2.0
27
- Add documentation website
38
- Reference documentation in `package.json`

Documentation/articles/GettingStarted.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ The tool will create new button in the Editor at `Tools / Generate Resource Path
1919
![Generate Button](~/images/intro/GenerateButton.png)
2020

2121
If your click the button the helper class will be generated in the root of the `Assets` folder
22+
23+
## Name generation rules
24+
25+
The generated method and filed names are slightly different from the actual file names, because not all valid file names are valid C# identifier names. The following rules apply:
26+
27+
- Whitespaces are removed
28+
- If the file name starts with a number a `_` character is added to the start of the identifier
29+
- All non-alphanumeric characters are replaced by the `_` character

Documentation/articles/KnownIssues.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 0.1.0 - *
44

5+
- Duplicate file names in the same class module will break the generated file. The solution for this requires further investigation
6+
7+
## 0.1.0 - 0.2.0: Fixed in 0.3.0
8+
59
- SpecialCharacters can break generated names.
610
- Numbers at the end of the files are supported, but not at the start
711
- Any character in the filename which would not be a valid C# filed/property/method name will break the generated code
@@ -12,5 +16,3 @@
1216
- `1Coin` does not
1317
- `!Coin` does not
1418
- `Coin!` does not
15-
16-
- Duplicate file names in the same class module will break the generated file. The solution for this requires further investigation

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator.Sample/ResourcePaths.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AutSoft.UnityResourceGenerator.Sample
44
{
55
// ReSharper disable PartialTypeWithSinglePart
6+
// ReSharper disable InconsistentNaming
67
public static partial class ResourcePaths
78
{
89
public static partial class Scenes
@@ -28,13 +29,26 @@ public static partial class Materials
2829
public static Material LoadCube() => Resources.Load<Material>(Cube);
2930
public const string CubeAlt = "CubeAlt";
3031
public static Material LoadCubeAlt() => Resources.Load<Material>(CubeAlt);
32+
public const string LiberationSansSDF_DropShadow = "Fonts & Materials/LiberationSans SDF - Drop Shadow";
33+
public static Material LoadLiberationSansSDF_DropShadow() => Resources.Load<Material>(LiberationSansSDF_DropShadow);
34+
public const string LiberationSansSDF_Outline = "Fonts & Materials/LiberationSans SDF - Outline";
35+
public static Material LoadLiberationSansSDF_Outline() => Resources.Load<Material>(LiberationSansSDF_Outline);
3136
}
3237
public static partial class AudioClips
3338
{
3439
public const string CoinSpin = "Coin Spin";
3540
public static AudioClip LoadCoinSpin() => Resources.Load<AudioClip>(CoinSpin);
41+
public const string _1Coin1 = "1Coin 1";
42+
public static AudioClip Load_1Coin1() => Resources.Load<AudioClip>(_1Coin1);
3643
public const string Coin = "Coin";
3744
public static AudioClip LoadCoin() => Resources.Load<AudioClip>(Coin);
3845
}
46+
public static partial class TextAssets
47+
{
48+
public const string LineBreakingFollowingCharacters = "LineBreaking Following Characters";
49+
public static TextAsset LoadLineBreakingFollowingCharacters() => Resources.Load<TextAsset>(LineBreakingFollowingCharacters);
50+
public const string LineBreakingLeadingCharacters = "LineBreaking Leading Characters";
51+
public static TextAsset LoadLineBreakingLeadingCharacters() => Resources.Load<TextAsset>(LineBreakingLeadingCharacters);
52+
}
3953
}
4054
}
Binary file not shown.

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator.Sample/Resources/1Coin 1.wav.meta

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/Generation/Modules/AllResources.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
using System.IO;
55
using System.Linq;
66
using System.Text;
7+
using System.Text.RegularExpressions;
78

89
namespace AutSoft.UnityResourceGenerator.Editor.Generation.Modules
910
{
1011
public sealed class AllResources : IModuleGenerator
1112
{
13+
private static readonly Regex NonAlphaNumeric = new Regex(@"[^a-zA-Z0-9]", RegexOptions.Compiled, TimeSpan.FromSeconds(1));
14+
private static readonly Regex StartsWithNumber = new Regex(@"^\d", RegexOptions.Compiled, TimeSpan.FromSeconds(1));
15+
1216
public string Generate(ResourceContext context) =>
1317
new StringBuilder()
1418
.AppendMultipleLines(context.Data.Select(d => Generate(context, d)))
@@ -47,9 +51,15 @@ public static partial class {data.ClassName}
4751
)
4852
.Replace('\\', '/');
4953

54+
var name = Path.GetFileNameWithoutExtension(filePath).Replace(" ", string.Empty);
55+
56+
if (StartsWithNumber.IsMatch(name)) name = name.Insert(0, "_");
57+
58+
name = NonAlphaNumeric.Replace(name, "_");
59+
5060
return
5161
(
52-
name: Path.GetFileNameWithoutExtension(filePath).Replace(" ", string.Empty),
62+
name,
5363
path: resourcePath,
5464
fileExtension: Path.GetExtension(filePath)
5565
);

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/Editor/Generation/ResourceFileGenerator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static string CreateResourceFile(ResourceContext context)
1515
namespace {0}
1616
{
1717
// ReSharper disable PartialTypeWithSinglePart
18+
// ReSharper disable InconsistentNaming
1819
public static partial class {1}
1920
{";
2021

@@ -27,6 +28,7 @@ public static partial class {1}
2728
const string fileBeginNoNamespace =
2829
@"
2930
// ReSharper disable PartialTypeWithSinglePart
31+
// ReSharper disable InconsistentNaming
3032
public static partial class {1}
3133
{";
3234

UnityResourceGenerator/Assets/AutSoft.UnityResourceGenerator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.autsoft.unityresourcegenerator",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"displayName": "Unity Resource Generator",
55
"description": "Generate path strings for Resources.Load",
66
"license": "MIT",

0 commit comments

Comments
 (0)