2026-08-10 14:57:50 +01:00
|
|
|
using System.Buffers.Text;
|
2021-03-10 09:18:07 +00:00
|
|
|
using System.Collections.Generic;
|
2021-03-11 11:18:21 +00:00
|
|
|
using System.IO;
|
2021-03-10 09:18:07 +00:00
|
|
|
using System.Linq;
|
2026-08-10 14:57:50 +01:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using MessagePack;
|
2026-09-14 17:39:12 +02:00
|
|
|
using CultMath;
|
2026-06-16 20:26:00 +01:00
|
|
|
using UnityEngine;
|
2026-08-10 14:57:50 +01:00
|
|
|
using UnityEditor;
|
2026-09-14 17:39:12 +02:00
|
|
|
using Random = CultMath.Random;
|
2021-03-10 09:18:07 +00:00
|
|
|
|
|
|
|
|
public class NameTools : EditorWindow
|
|
|
|
|
{
|
|
|
|
|
public int NameGeneratorMinLength = 5;
|
|
|
|
|
public int NameGeneratorMaxLength = 10;
|
|
|
|
|
public int NameGeneratorOrder = 4;
|
2026-08-10 14:57:50 +01:00
|
|
|
private int minWordLength = 4;
|
|
|
|
|
private TextAsset nameFile;
|
2021-03-10 09:18:07 +00:00
|
|
|
private MarkovNameGenerator _nameGenerator;
|
2021-03-11 11:18:21 +00:00
|
|
|
private bool _stripNumberTokens;
|
2021-03-10 09:18:07 +00:00
|
|
|
|
2026-08-10 14:57:50 +01:00
|
|
|
|
|
|
|
|
// Add menu named "My Window" to the Window menu
|
2021-03-10 09:18:07 +00:00
|
|
|
[MenuItem("Window/Aetheria/Name Tools")]
|
2026-08-10 14:57:50 +01:00
|
|
|
static void Init()
|
2021-03-10 09:18:07 +00:00
|
|
|
{
|
2026-08-10 14:57:50 +01:00
|
|
|
// Get existing open window or if none, make a new one:
|
|
|
|
|
NameTools window = (NameTools)EditorWindow.GetWindow(typeof(NameTools));
|
2021-03-10 09:18:07 +00:00
|
|
|
window.Show();
|
|
|
|
|
}
|
2026-08-10 14:57:50 +01:00
|
|
|
|
|
|
|
|
bool HasNonASCIIChars(string str)
|
2026-06-16 20:26:00 +01:00
|
|
|
{
|
2026-08-10 14:57:50 +01:00
|
|
|
return (System.Text.Encoding.UTF8.GetByteCount(str) != str.Length);
|
2026-06-16 20:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
2026-08-10 14:57:50 +01:00
|
|
|
void OnGUI()
|
2021-03-16 11:06:54 +00:00
|
|
|
{
|
2026-08-10 14:57:50 +01:00
|
|
|
|
|
|
|
|
nameFile = (TextAsset) EditorGUILayout.ObjectField("Name File", nameFile, typeof(TextAsset), false);
|
|
|
|
|
minWordLength = EditorGUILayout.IntField("Minimum File Word Length", minWordLength);
|
|
|
|
|
NameGeneratorMinLength = EditorGUILayout.IntField("Generated Minimum Word Length", NameGeneratorMinLength);
|
|
|
|
|
NameGeneratorMaxLength = EditorGUILayout.IntField("Generated Maximum Word Length", NameGeneratorMaxLength);
|
|
|
|
|
NameGeneratorOrder = EditorGUILayout.IntField("Generator Order", NameGeneratorOrder);
|
|
|
|
|
_stripNumberTokens = GUILayout.Toggle(_stripNumberTokens, "Strip Number Tokens");
|
|
|
|
|
|
|
|
|
|
if (GUILayout.Button("Clean Name File"))
|
2021-03-10 09:18:07 +00:00
|
|
|
{
|
2026-08-10 14:57:50 +01:00
|
|
|
var lines = nameFile.text.Split('\n');
|
|
|
|
|
using StreamWriter outputFile = new StreamWriter(Path.Combine(Application.dataPath, nameFile.name + ".csv"));
|
|
|
|
|
var names = new HashSet<string>();
|
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
{
|
|
|
|
|
var tokens = line.Split(',', ' ');
|
|
|
|
|
foreach (var t in tokens)
|
|
|
|
|
{
|
|
|
|
|
if (!HasNonASCIIChars(t))
|
|
|
|
|
{
|
|
|
|
|
var s = new string(t.Where(c => char.IsLetter(c) || c == '-' || c == '`' || c == '\'').ToArray()).Trim().Trim('`','-');
|
|
|
|
|
if(s.Length >= minWordLength && !names.Contains(s))
|
|
|
|
|
{
|
|
|
|
|
names.Add(s);
|
|
|
|
|
outputFile.WriteLine(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-16 20:26:00 +01:00
|
|
|
}
|
|
|
|
|
|
2026-08-10 14:57:50 +01:00
|
|
|
if (GUILayout.Button("Process Name File") && nameFile != null)
|
2026-06-16 20:26:00 +01:00
|
|
|
{
|
2026-08-10 14:57:50 +01:00
|
|
|
var names = new HashSet<string>();
|
|
|
|
|
var lines = nameFile.text.Split('\n');
|
|
|
|
|
foreach (var line in lines)
|
2021-03-10 09:18:07 +00:00
|
|
|
{
|
2026-08-10 14:57:50 +01:00
|
|
|
foreach(var word in line.ToUpperInvariant().Split(' ', ',', '.', '"'))
|
|
|
|
|
if (word.Length >= minWordLength && !names.Contains(word))
|
|
|
|
|
names.Add(word);
|
2021-03-10 09:18:07 +00:00
|
|
|
}
|
2026-08-10 14:57:50 +01:00
|
|
|
Debug.Log($"Found {lines.Length} lines, with {names.Count} unique names!");
|
|
|
|
|
var random = new Random(1337);
|
|
|
|
|
_nameGenerator = new MarkovNameGenerator(ref random, names, NameGeneratorOrder, NameGeneratorMinLength, NameGeneratorMaxLength);
|
2021-03-10 09:18:07 +00:00
|
|
|
}
|
2026-06-16 20:26:00 +01:00
|
|
|
|
2026-08-10 14:57:50 +01:00
|
|
|
if (_nameGenerator != null)
|
2026-06-16 20:26:00 +01:00
|
|
|
{
|
2026-08-10 14:57:50 +01:00
|
|
|
if (GUILayout.Button("Generate Name"))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(_nameGenerator.NextName);
|
|
|
|
|
}
|
2026-06-16 20:26:00 +01:00
|
|
|
}
|
2021-03-10 09:18:07 +00:00
|
|
|
}
|
2026-08-10 14:57:50 +01:00
|
|
|
}
|