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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [1.3.4] - 2026-11-07

### Fixed
- Deleting table collection assets now always deletes their generated files (#14). Before, the files stayed when
the last collection was deleted, or when the collection assets were deleted in an order that left a collection
without its shared table data.

### Changed
- Table edits (adding, removing or editing keys, entries and collections) no longer regenerate immediately; the
code regenerates when the table is saved (**File > Save Project** or Ctrl+S).

## [1.3.3] - 2026-10-28

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Severnbli.LocalizationCodegen.Installer
public static partial class Installer
{
public const string PackageId = "com.severnbli.localization-codegen";
public const string Version = "1.3.3";
public const string Version = "1.3.4";

static Installer()
{
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ label.text = Ui.HelloWorld;
| 💬 **Smart strings** | Entries with placeholders become methods with typed positional and named parameters |
| 🔗 **Reference members** | Optional `Ref` class with `LocalizedString` / `LocalizedAsset<T>` references that follow locale changes |
| ⚡ **UniTask** | Optional `…Async` members returning `UniTask`, with cancellation |
| 🔁 **Automatic regeneration** | Debounced regeneration after table edits, including changes pulled from version control |
| 🔁 **Automatic regeneration** | Regeneration when tables are saved, including changes pulled from version control |
| 🧪 **CI ready** | Batch mode entry points to regenerate, or to fail when generated code is out of date |
| 🪄 **First-run wizard** | A setup wizard opens after the package is first imported |

Expand All @@ -79,7 +79,7 @@ label.text = Ui.HelloWorld;
label.text = Ui.HelloWorld;
```

Edit your tables as usual – the classes regenerate on their own.
Edit your tables as usual – the classes regenerate when you save them.

## Installation

Expand Down Expand Up @@ -134,7 +134,7 @@ Add the scoped registry and the dependency to `Packages/manifest.json`:
}
],
"dependencies": {
"com.severnbli.localization-codegen": "1.3.3"
"com.severnbli.localization-codegen": "1.3.4"
}
}
```
Expand Down Expand Up @@ -378,7 +378,7 @@ Open **Project Settings > Localization Codegen**, or **Window > Localization Cod
| Output Folder | `Assets/Generated/Localization` | Must be inside `Assets`. Changing it removes generated files from the old folder. Inside another `.asmdef`, you are offered to add the references the code needs. |
| String Tables | on | Generate classes for string table collections. Disabling deletes the matching files on the next run. |
| Asset Tables | on | Generate classes for asset table collections. Disabling deletes the matching files on the next run. |
| Regenerate On Changes | on | Debounced automatic regeneration after table edits and external (VCS) changes. |
| Regenerate On Changes | on | Automatic regeneration when table assets are saved (**File > Save Project** or Ctrl+S) and after external (VCS) changes. |
| Silently On Settings Change | off | Regenerate without asking when a setting that changes the generated code is edited. Off asks first. Needs **Regenerate On Changes**. |
| On Metadata Edit | off | Regenerate after every edit of the Smart Parameter Types metadata. Off regenerates when the table is saved. Needs **Regenerate On Changes**. |
| Generate Reference Members | off | Emit the nested [`Ref` class](#reference-members). |
Expand Down
11 changes: 11 additions & 0 deletions Unity-Package/Assets/root/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [1.3.4] - 2026-11-07

### Fixed
- Deleting table collection assets now always deletes their generated files (#14). Before, the files stayed when
the last collection was deleted, or when the collection assets were deleted in an order that left a collection
without its shared table data.

### Changed
- Table edits (adding, removing or editing keys, entries and collections) no longer regenerate immediately; the
code regenerates when the table is saved (**File > Save Project** or Ctrl+S).

## [1.3.3] - 2026-10-28

### Added
Expand Down
27 changes: 25 additions & 2 deletions Unity-Package/Assets/root/Editor/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public sealed class GenerateOptions

/// <summary>Allow a run inside <c>-batchmode</c>.</summary>
public bool AllowInBatchMode;

/// <summary>
/// The run follows the deletion of table collection assets, so collections that are gone (or lost their
/// shared table data) were removed on purpose and their generated files are deleted, even the last one.
/// </summary>
public bool TablesDeleted;
}

/// <summary>Result of one generation run.</summary>
Expand Down Expand Up @@ -103,7 +109,7 @@ public static GenerateResult Run(GenerateOptions options)
WarnAboutOwningAssembly(folder, emitOptions, settings, result);

var reader = new TableReader(emitOptions);
var read = reader.Read(settings.GenerateStringTables, settings.GenerateAssetTables);
var read = reader.Read(settings.GenerateStringTables, settings.GenerateAssetTables, options.TablesDeleted);
result.Warnings.AddRange(read.Warnings);
result.Total = read.Models.Count;

Expand Down Expand Up @@ -155,7 +161,8 @@ public static GenerateResult Run(GenerateOptions options)

var allowCleanup = read.Failed == 0;

if (read.Models.Count == 0 && HasPreviousOutput(settings, folderFull))
// A deliberate deletion of the last collections needs no confirmation: deleting the tables was it.
if (read.Models.Count == 0 && HasPreviousOutput(settings, folderFull) && !options.TablesDeleted)
{
if (options.IsAutomatic || !options.PromptOnMassDeletion || Application.isBatchMode)
{
Expand Down Expand Up @@ -253,6 +260,10 @@ public static GenerateResult Run(GenerateOptions options)
.Select(p => p.Key + "|" + p.Value)
.ToArray();
settings.LastModelHash = HashModels(read.Models, emitOptions);
settings.TableAssetPaths = read.AssetPaths
.Distinct(StringComparer.Ordinal)
.OrderBy(p => p, StringComparer.Ordinal)
.ToArray();
settings.SaveSettings();
}
finally
Expand Down Expand Up @@ -648,6 +659,18 @@ public static string ComputeModelHash()
var options = BuildEmitOptions(settings);
var reader = new TableReader(options);
var read = reader.Read(settings.GenerateStringTables, settings.GenerateAssetTables);

// Projects generated by older versions have no table paths yet; without them a deletion of the
// tables would not be recognised before the next generation run.
if (settings.TableAssetPaths.Length == 0 && read.AssetPaths.Count > 0)
{
settings.TableAssetPaths = read.AssetPaths
.Distinct(StringComparer.Ordinal)
.OrderBy(p => p, StringComparer.Ordinal)
.ToArray();
settings.SaveSettings();
}

CodeEmitter.AssignClassNames(read.Models, options);
return HashModels(read.Models, options);
}
Expand Down
41 changes: 36 additions & 5 deletions Unity-Package/Assets/root/Editor/CodegenAssetPostprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace Severnbli.LocalizationCodegen.Editor
{
/// <summary>
/// Detects table assets changed outside the editor (git pull, merge, revert) and marks the output stale.
/// Detects saved or imported table assets (table save in the editor, git pull, merge, revert) and marks the
/// output stale. This is the only trigger for automatic regeneration on table changes.
/// </summary>
internal sealed class CodegenAssetPostprocessor : AssetPostprocessor
{
Expand All @@ -22,10 +23,14 @@ static void OnPostprocessAllAssets(
if (!settings.SetupCompleted || !settings.AutoRegenerate)
return;

if (ContainsLocalizationAsset(importedAssets) ||
ContainsLocalizationAsset(movedAssets) ||
ContainsAssetPath(deletedAssets) ||
ContainsAssetPath(movedFromAssetPaths))
if (ContainsTableAsset(deletedAssets, settings.TableAssetPaths))
{
CodegenHooks.MarkTablesDeleted();
}
else if (ContainsLocalizationAsset(importedAssets) ||
ContainsLocalizationAsset(movedAssets) ||
ContainsAssetPath(deletedAssets) ||
ContainsAssetPath(movedFromAssetPaths))
{
CodegenHooks.MarkDirtyExternal();
}
Expand Down Expand Up @@ -59,6 +64,32 @@ static bool ContainsLocalizationAsset(string[] paths)
return false;
}

/// <summary>
/// True when a deleted path is a collection or shared table data asset the last run read, or a folder that
/// held one. Deleted assets can no longer be loaded, so their type is known only from that list.
/// </summary>
static bool ContainsTableAsset(string[] deletedPaths, string[] tableAssetPaths)
{
if (deletedPaths == null || tableAssetPaths == null || tableAssetPaths.Length == 0)
return false;

foreach (var deleted in deletedPaths)
{
if (string.IsNullOrEmpty(deleted))
continue;
var folder = deleted.TrimEnd('/') + "/";
foreach (var table in tableAssetPaths)
{
if (string.IsNullOrEmpty(table))
continue;
if (string.Equals(table, deleted, StringComparison.Ordinal) ||
table.StartsWith(folder, StringComparison.Ordinal))
return true;
}
}
return false;
}

static bool ContainsAssetPath(string[] paths)
{
if (paths == null)
Expand Down
35 changes: 15 additions & 20 deletions Unity-Package/Assets/root/Editor/CodegenHooks.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using UnityEditor;
using UnityEditor.Localization;
using UnityEngine;

namespace Severnbli.LocalizationCodegen.Editor
{
/// <summary>
/// Coalesces localization editor events into one debounced generation pass.
/// Coalesces dirty marks into one debounced generation pass. Table edits mark the output dirty only once the
/// table assets are saved (see <see cref="CodegenAssetPostprocessor"/>), not on every in-memory edit.
/// The dirty flag lives in <see cref="SessionState"/> so it survives a domain reload.
/// </summary>
[InitializeOnLoad]
internal static class CodegenHooks
{
const string DirtyKey = "LocalizationCodegen.Dirty";
const string WizardShownKey = "LocalizationCodegen.WizardShown";
const string TablesDeletedKey = "LocalizationCodegen.TablesDeleted";

const double DebounceSeconds = 0.5;

Expand All @@ -24,19 +25,6 @@ static CodegenHooks()
var settings = CodegenSettings.instance;
settings.EnsureMigrated();

var events = LocalizationEditorSettings.EditorEvents;
events.CollectionAdded += _ => MarkDirtyExternal();
events.CollectionRemoved += _ => MarkDirtyExternal();
events.CollectionModified += (_, __) => MarkDirtyExternal();
events.TableEntryAdded += (_, __) => MarkDirtyExternal();
events.TableEntryRemoved += (_, __) => MarkDirtyExternal();
events.TableEntryModified += _ => MarkDirtyExternal();
events.AssetTableEntryAdded += (_, __, ___) => MarkDirtyExternal();
events.AssetTableEntryRemoved += (_, __, ___, ____) => MarkDirtyExternal();
events.LocaleAdded += _ => MarkDirtyExternal();
events.LocaleRemoved += _ => MarkDirtyExternal();
events.LocaleSortOrderChanged += (_, __) => MarkDirtyExternal();

if (SessionState.GetBool(DirtyKey, false))
s_LastDirtyTime = EditorApplication.timeSinceStartup;

Expand All @@ -51,6 +39,13 @@ internal static void MarkDirtyExternal()
s_LastDirtyTime = EditorApplication.timeSinceStartup;
}

/// <summary>Marks the output stale after table collection assets were deleted, so their files go too.</summary>
internal static void MarkTablesDeleted()
{
SessionState.SetBool(TablesDeletedKey, true);
MarkDirtyExternal();
}

static void Tick()
{
if (Application.isBatchMode)
Expand All @@ -77,14 +72,14 @@ static void Tick()
return;

var settings = CodegenSettings.instance;
var tablesDeleted = SessionState.GetBool(TablesDeletedKey, false);
SessionState.SetBool(DirtyKey, false);
SessionState.SetBool(TablesDeletedKey, false);

if (!settings.SetupCompleted || !settings.AutoRegenerate)
{
SessionState.SetBool(DirtyKey, false);
return;
}

SessionState.SetBool(DirtyKey, false);
CodeGenerator.Run(new GenerateOptions { IsAutomatic = true });
CodeGenerator.Run(new GenerateOptions { IsAutomatic = true, TablesDeleted = tablesDeleted });
}

static void CheckStaleness()
Expand Down
3 changes: 3 additions & 0 deletions Unity-Package/Assets/root/Editor/CodegenSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public sealed class CodegenSettings : ScriptableSingleton<CodegenSettings>
[SerializeField] private string lastOutputFolder;
[SerializeField] private string[] generatedPaths = new string[0];
[SerializeField] private string lastAsmdefPath;
// Collection and shared table data assets seen by the last run, to recognise their deletion.
[SerializeField] private string[] tableAssetPaths = new string[0];

// Incremental generation (B8) and staleness (A6).
[SerializeField] private string lastModelHash;
Expand Down Expand Up @@ -57,6 +59,7 @@ public sealed class CodegenSettings : ScriptableSingleton<CodegenSettings>
public string LastOutputFolder { get => lastOutputFolder; set => lastOutputFolder = value; }
public string[] GeneratedPaths { get => generatedPaths ?? (generatedPaths = new string[0]); set => generatedPaths = value; }
public string LastAsmdefPath { get => lastAsmdefPath; set => lastAsmdefPath = value; }
public string[] TableAssetPaths { get => tableAssetPaths ?? (tableAssetPaths = new string[0]); set => tableAssetPaths = value; }
public string LastModelHash { get => lastModelHash; set => lastModelHash = value; }
public string[] CollectionHashes { get => collectionHashes ?? (collectionHashes = new string[0]); set => collectionHashes = value; }

Expand Down
2 changes: 1 addition & 1 deletion Unity-Package/Assets/root/Editor/SettingsGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static class Content
public static readonly GUIContent AssetTables = new GUIContent("Asset Tables",
"Generate classes for asset table collections. Disabling this deletes the matching generated files on the next run.");
public static readonly GUIContent AutoRegenerate = new GUIContent("Regenerate On Changes",
"Automatically regenerate when table collections or entries change.");
"Automatically regenerate when table assets are saved or change on disk.");
public static readonly GUIContent RegenerateSilently = new GUIContent("Silently On Settings Change",
"Regenerate without asking when a setting that changes the generated code is edited. " +
"Off asks first.");
Expand Down
32 changes: 31 additions & 1 deletion Unity-Package/Assets/root/Editor/TableReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal sealed class ReaderResult
public readonly List<TableModel> Models = new List<TableModel>();
public readonly List<string> Warnings = new List<string>();
public int Failed;

/// <summary>Asset paths of the collections read and of their shared table data.</summary>
public readonly List<string> AssetPaths = new List<string>();
}

/// <summary>
Expand All @@ -37,14 +40,21 @@ public TableReader(EmitOptions options)
CollectGlobalVariableGroups();
}

public ReaderResult Read(bool readStringTables, bool readAssetTables)
/// <param name="tablesDeleted">
/// Table assets were just deleted, so a collection without shared table data is being removed: it is skipped
/// as if it no longer existed instead of counting as a failed read that would block cleanup.
/// </param>
public ReaderResult Read(bool readStringTables, bool readAssetTables, bool tablesDeleted = false)
{
var result = new ReaderResult();

if (readStringTables)
{
foreach (var collection in Sort(LocalizationEditorSettings.GetStringTableCollections()))
{
AddAssetPaths(collection, result);
if (tablesDeleted && collection.SharedData == null)
continue;
try
{
result.Models.Add(ReadCollection(collection, false, result.Warnings));
Expand All @@ -61,6 +71,9 @@ public ReaderResult Read(bool readStringTables, bool readAssetTables)
{
foreach (var collection in Sort(LocalizationEditorSettings.GetAssetTableCollections()))
{
AddAssetPaths(collection, result);
if (tablesDeleted && collection.SharedData == null)
continue;
try
{
result.Models.Add(ReadCollection(collection, true, result.Warnings));
Expand All @@ -76,6 +89,23 @@ public ReaderResult Read(bool readStringTables, bool readAssetTables)
return result;
}

static void AddAssetPaths(LocalizationTableCollection collection, ReaderResult result)
{
try
{
var path = AssetDatabase.GetAssetPath(collection);
if (!string.IsNullOrEmpty(path))
result.AssetPaths.Add(path);
var sharedPath = collection.SharedData != null ? AssetDatabase.GetAssetPath(collection.SharedData) : null;
if (!string.IsNullOrEmpty(sharedPath))
result.AssetPaths.Add(sharedPath);
}
catch
{
// The paths only help to recognise deletions; a broken collection is reported by the read itself.
}
}

static IEnumerable<LocalizationTableCollection> Sort(IEnumerable<LocalizationTableCollection> collections)
{
return (collections ?? Enumerable.Empty<LocalizationTableCollection>())
Expand Down
Loading
Loading