Skip to content
Merged
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
10 changes: 6 additions & 4 deletions LuYao.ResourcePacker.SourceGenerator/ResourcePackageGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ private static string GenerateSourceCode(string assemblyName, string className,
var safeKey = key; // Already safe from ResourceKeyHelper
var methodName = $"Read{Capitalize(safeKey)}";
var asyncMethodName = $"Read{Capitalize(safeKey)}Async";
var asyncStringMethodName = $"Read{Capitalize(safeKey)}AsStringAsync";
var stringMethodName = $"Read{Capitalize(safeKey)}AsString";

// Async byte array method
sb.AppendLine($" /// <summary>");
Expand All @@ -244,7 +246,7 @@ private static string GenerateSourceCode(string assemblyName, string className,
sb.AppendLine($" /// <summary>");
sb.AppendLine($" /// Reads the '{key}' resource as a string asynchronously using UTF-8 encoding.");
sb.AppendLine($" /// </summary>");
sb.AppendLine($" public static System.Threading.Tasks.Task<string> {asyncMethodName}AsString()");
sb.AppendLine($" public static System.Threading.Tasks.Task<string> {asyncStringMethodName}()");
sb.AppendLine($" {{");
sb.AppendLine($" return Reader.ReadResourceAsStringAsync(Keys.{safeKey});");
sb.AppendLine($" }}");
Expand All @@ -255,7 +257,7 @@ private static string GenerateSourceCode(string assemblyName, string className,
sb.AppendLine($" /// Reads the '{key}' resource as a string asynchronously using the specified encoding.");
sb.AppendLine($" /// </summary>");
sb.AppendLine($" /// <param name=\"encoding\">The encoding to use when converting bytes to string.</param>");
sb.AppendLine($" public static System.Threading.Tasks.Task<string> {asyncMethodName}AsString(System.Text.Encoding encoding)");
sb.AppendLine($" public static System.Threading.Tasks.Task<string> {asyncStringMethodName}(System.Text.Encoding encoding)");
sb.AppendLine($" {{");
sb.AppendLine($" return Reader.ReadResourceAsStringAsync(Keys.{safeKey}, encoding);");
sb.AppendLine($" }}");
Expand All @@ -265,7 +267,7 @@ private static string GenerateSourceCode(string assemblyName, string className,
sb.AppendLine($" /// <summary>");
sb.AppendLine($" /// Reads the '{key}' resource as a string synchronously using UTF-8 encoding.");
sb.AppendLine($" /// </summary>");
sb.AppendLine($" public static string {methodName}AsString()");
sb.AppendLine($" public static string {stringMethodName}()");
sb.AppendLine($" {{");
sb.AppendLine($" return Reader.ReadResourceAsString(Keys.{safeKey});");
sb.AppendLine($" }}");
Expand All @@ -276,7 +278,7 @@ private static string GenerateSourceCode(string assemblyName, string className,
sb.AppendLine($" /// Reads the '{key}' resource as a string synchronously using the specified encoding.");
sb.AppendLine($" /// </summary>");
sb.AppendLine($" /// <param name=\"encoding\">The encoding to use when converting bytes to string.</param>");
sb.AppendLine($" public static string {methodName}AsString(System.Text.Encoding encoding)");
sb.AppendLine($" public static string {stringMethodName}(System.Text.Encoding encoding)");
sb.AppendLine($" {{");
sb.AppendLine($" return Reader.ReadResourceAsString(Keys.{safeKey}, encoding);");
sb.AppendLine($" }}");
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ Console.WriteLine(R.Keys.config);
Console.WriteLine(R.Keys.template);

// Read resources using generated methods
string message = await R.ReadMessageAsyncAsString();
string message = await R.ReadMessageAsStringAsync();
byte[] configBytes = await R.ReadConfigAsync();
string template = await R.ReadTemplateAsyncAsString();
string template = await R.ReadTemplateAsStringAsync();

// Access the underlying reader if needed
ResourcePackageReader reader = R.Reader;
Expand Down Expand Up @@ -170,10 +170,10 @@ namespace YourAssembly
public static ResourcePackageReader Reader { get; }

public static Task<byte[]> ReadTestAsync() { ... }
public static Task<string> ReadTestAsyncAsString() { ... }
public static Task<string> ReadTestAsStringAsync() { ... }

public static Task<byte[]> ReadConfigAsync() { ... }
public static Task<string> ReadConfigAsyncAsString() { ... }
public static Task<string> ReadConfigAsStringAsync() { ... }
}
}
```
Expand Down
16 changes: 8 additions & 8 deletions docs/blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ Console.WriteLine(R.Keys.config);
Console.WriteLine(R.Keys.template);

// 使用生成的强类型方法读取资源
string message = await R.ReadMessageAsyncAsString();
string message = await R.ReadMessageAsStringAsync();
byte[] configBytes = await R.ReadConfigAsync();
string template = await R.ReadTemplateAsyncAsString();
string template = await R.ReadTemplateAsStringAsync();

// 如果需要,也可以访问底层的 Reader
ResourcePackageReader reader = R.Reader;
Expand Down Expand Up @@ -266,7 +266,7 @@ using var stream = reader.GetStream("large-text-file");
3. **代码生成**:生成包含以下内容的 `R.g.cs` 文件:
- `R.Keys` 嵌套类:包含所有资源键常量
- `R.Reader` 静态属性:提供 `ResourcePackageReader` 实例
- 强类型方法:为每个资源生成 `ReadXxxAsync()` 和 `ReadXxxAsyncAsString()` 方法
- 强类型方法:为每个资源生成 `ReadXxxAsync()` 和 `ReadXxxAsStringAsync()` 方法

### 命名空间处理

Expand Down Expand Up @@ -326,7 +326,7 @@ Resources/

```csharp
// ✅ 推荐:强类型,有智能提示
var content = await R.ReadAppConfigAsyncAsString();
var content = await R.ReadAppConfigAsStringAsync();

// ❌ 不推荐:魔法字符串,容易出错
var content = await reader.ReadResourceAsStringAsync("app-config");
Expand All @@ -337,7 +337,7 @@ var content = await reader.ReadResourceAsStringAsync("app-config");
**同一项目内的资源访问**:
```csharp
// 在项目内部直接使用 R 类
var data = await R.ReadDataAsyncAsString();
var data = await R.ReadDataAsStringAsync();
```

**跨程序集访问的限制**:
Expand All @@ -347,10 +347,10 @@ var data = await R.ReadDataAsyncAsString();
1. **推荐方案**:在每个需要资源的项目中独立管理资源
```csharp
// Project1 中
var config1 = await R.ReadConfigAsyncAsString();
var config1 = await R.ReadConfigAsStringAsync();

// Project2 中
var config2 = await R.ReadConfigAsyncAsString();
var config2 = await R.ReadConfigAsStringAsync();
```

2. **传递数据而非 R 类**:如果确实需要跨项目共享资源,可以在资源所在的项目中读取后传递数据
Expand All @@ -360,7 +360,7 @@ var data = await R.ReadDataAsyncAsString();
{
public static async Task<string> GetConfigAsync()
{
return await R.ReadConfigAsyncAsString();
return await R.ReadConfigAsStringAsync();
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/ExampleProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ static async Task Main(string[] args)

// Read resources using generated methods
Console.WriteLine("--- Reading 'config' using generated method ---");
string config = await R.ReadConfigAsyncAsString();
string config = await R.ReadConfigAsStringAsync();
Console.WriteLine(config);
Console.WriteLine();

Console.WriteLine("--- Reading 'message' using generated method ---");
string messageGenerated = await R.ReadMessageAsyncAsString();
string messageGenerated = await R.ReadMessageAsStringAsync();
Console.WriteLine(messageGenerated);
Console.WriteLine();

Expand Down
4 changes: 2 additions & 2 deletions examples/RootNamespaceTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public static async Task Main(string[] args)
// Test reading resources
try
{
var sampleText = await R.ReadSampleAsyncAsString();
var sampleText = await R.ReadSampleAsStringAsync();
Console.WriteLine($"Sample resource content: {sampleText}");

var configJson = await R.ReadConfigAsyncAsString();
var configJson = await R.ReadConfigAsStringAsync();
Console.WriteLine($"Config resource content: {configJson}");

Console.WriteLine();
Expand Down