-
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
12 changed files
with
229 additions
and
41 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
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// R/WinRT (C#) | ||
// | ||
// Copyright (C) mntone. | ||
// Licensed under the MIT License. | ||
|
||
#nullable enable | ||
|
||
using Microsoft.Windows.ApplicationModel.Resources; | ||
|
||
namespace RWinRT | ||
{ | ||
internal abstract class ResourceManager | ||
{ | ||
private ResourceMap Resources { get; } | ||
|
||
protected ResourceManager(string resourceName) | ||
{ | ||
Resources = Native.MainResourceMap.GetSubtree(resourceName); | ||
} | ||
|
||
public string GetValueCore(string key) | ||
{ | ||
return Resources.GetValue(key, Context).ValueAsString; | ||
} | ||
|
||
public string GetValueCore(string key, ResourceContext context) | ||
{ | ||
return Resources.GetValue(key, context).ValueAsString; | ||
} | ||
|
||
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public string GetFormatValueCore(string key, params object?[] args) | ||
#else | ||
public string GetFormatValueCore(string key, params object[] args) | ||
#endif | ||
{ | ||
var format = Resources.GetValue(key, Context).ValueAsString; | ||
return string.Format(format, args); | ||
} | ||
|
||
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public string GetFormatValueCore(string key, ResourceContext context, params object?[] args) | ||
#else | ||
public string GetFormatValueCore(string key, ResourceContext context, params object[] args) | ||
#endif | ||
{ | ||
var format = Resources.GetValue(key, context).ValueAsString; | ||
return string.Format(format, args); | ||
} | ||
|
||
protected static Microsoft.Windows.ApplicationModel.Resources.ResourceManager Native { get; } | ||
|
||
protected static ResourceContext Context { get; } | ||
|
||
static ResourceManager() | ||
{ | ||
Native = new Microsoft.Windows.ApplicationModel.Resources.ResourceManager(); | ||
Context = Native.CreateResourceContext(); | ||
} | ||
|
||
public static void ChangeLanguage(string language) | ||
{ | ||
Context.QualifierValues["Language"] = language; | ||
} | ||
} | ||
|
||
internal struct ResourceObject | ||
{ | ||
public ResourceManager Manager { get; } | ||
|
||
public string Key { get; } | ||
|
||
internal ResourceObject(ResourceManager manager, string key) | ||
{ | ||
Manager = manager; | ||
Key = key; | ||
} | ||
|
||
public string Value | ||
{ | ||
get { return Manager.GetValueCore(Key); } | ||
} | ||
|
||
public string ValueIn(ResourceContext context) | ||
{ | ||
return Manager.GetValueCore(Key, context); | ||
} | ||
|
||
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public string Format(params object?[] args) | ||
#else | ||
public string Format(params object[] args) | ||
#endif | ||
{ | ||
return Manager.GetFormatValueCore(Key, args); | ||
} | ||
|
||
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER | ||
public string Format(ResourceContext context, params object?[] args) | ||
#else | ||
public string Format(ResourceContext context, params object[] args) | ||
#endif | ||
{ | ||
return Manager.GetFormatValueCore(Key, context, args); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Mntone.RWinRT.Generators.CSharp.BlockWriters; | ||
using Mntone.RWinRT.Generators.UnitWriters; | ||
using System.Linq; | ||
|
||
namespace Mntone.RWinRT.Generators.CSharp | ||
{ | ||
public static class CSharpAutogen3 | ||
|
||
{ | ||
public static string[] Headers { get; } = | ||
{ | ||
"//------------------------------------------------------------------------------", | ||
"// <auto-generated>", | ||
$"// This file was generated by R/WinRT v{typeof(Program).Assembly.GetName().Version}", | ||
"//", | ||
"// Changes to this file may cause incorrect behavior and will be lost if", | ||
"// the code is regenerated.", | ||
"// </auto-generated>", | ||
"//------------------------------------------------------------------------------", | ||
"#nullable enable", | ||
"", | ||
"// R/WinRT (C#) / CSharpAutogen V3", | ||
"//", | ||
$"// Copyright (C) {System.DateTime.UtcNow.Year} {System.Diagnostics.FileVersionInfo.GetVersionInfo( typeof(Program).Assembly.Location).CompanyName}.", | ||
"// Licensed under the MIT License.", | ||
"", | ||
}; | ||
|
||
public static string[] Body { get; } = | ||
{ | ||
"private sealed class __<0>_ResourceManager : global::RWinRT.ResourceManager", | ||
"{", | ||
"<indent>public static __<0>_ResourceManager Instance { get; } = new __<0>_ResourceManager();", | ||
"<indent>public __<0>_ResourceManager() : base(\"<0>\") { }", | ||
"}", | ||
"", | ||
}; | ||
|
||
private static void WriteSection(CSharpWriterContext ctx, string type, ResourcesData data) | ||
{ | ||
using (StaticClass.Block(ctx, "R")) | ||
{ | ||
Raw.Write(ctx, Body.Select(v => v.Replace("<0>", type)).ToArray()); | ||
|
||
foreach (var resource in data.Resources) | ||
{ | ||
var name = resource.Name; | ||
var preferredName = ctx.PreferredNameConverter(name); | ||
Raw.Write(ctx, | ||
$"/// <summary>\"{resource.Value}\"</summary>", | ||
$"public static global::RWinRT.ResourceObject {preferredName} {{ get; }} = new global::RWinRT.ResourceObject(__{type}_ResourceManager.Instance, \"{name}\");"); | ||
} | ||
} | ||
} | ||
|
||
public static string Build(CSharpWriterContext ctx, ResourcesData defaultResource, ResourcesData[] resources) | ||
{ | ||
// Write headers | ||
Raw.Write(ctx, Headers); | ||
|
||
// Write root namespace | ||
using (Namespace.Block(ctx, ctx.RootNamespace)) | ||
{ | ||
WriteSection(ctx, defaultResource.ResourceType, defaultResource); | ||
foreach (var resource in resources) | ||
{ | ||
using (Namespace.Block(ctx, resource.ResourceType)) | ||
{ | ||
WriteSection(ctx, resource.ResourceType, resource); | ||
} | ||
} | ||
} | ||
return ctx.Builder.ToString(); | ||
} | ||
} | ||
} |
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