Skip to content

Commit

Permalink
Change to use YAML for definition files (#17)
Browse files Browse the repository at this point in the history
* Change to use YAML for definition files

* Convert to yaml file

* Fixed yaml load error
  • Loading branch information
shibayan authored May 2, 2021
1 parent 6d7e220 commit 5363eb9
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 135 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,6 @@ ASALocalRun/
.localhistory/

# ImoutoDesktop file
profile.xml
settings.xml
profile.yml
settings.yml
imouto_vars.txt
7 changes: 0 additions & 7 deletions resource/balloons/default_blue/balloon.xml

This file was deleted.

4 changes: 4 additions & 0 deletions resource/balloons/default_blue/balloon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id: default_blue
name: デフォルト - 青
userColor: '#6633ff'
imoutoColor: '#ff3366'
8 changes: 0 additions & 8 deletions resource/characters/kaede/character.xml

This file was deleted.

5 changes: 5 additions & 0 deletions resource/characters/kaede/character.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id: kaede
name: かえで
description: 旧デフォルト
age: 15
tsundereLevel: 5
8 changes: 0 additions & 8 deletions resource/characters/sakura/character.xml

This file was deleted.

5 changes: 5 additions & 0 deletions resource/characters/sakura/character.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id: sakura
name: さくら
description: 新デフォルト
age: 12
tsundereLevel: 3
8 changes: 0 additions & 8 deletions resource/characters/sumire/character.xml

This file was deleted.

5 changes: 5 additions & 0 deletions resource/characters/sumire/character.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id: sumire
name: すみれ
description: 名称未設定
age: 10
tsundereLevel: 5
13 changes: 6 additions & 7 deletions src/ImoutoDesktop/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
Expand Down Expand Up @@ -31,7 +30,7 @@ private void App_Startup(object sender, StartupEventArgs e)
#endif

// 設定ファイルを読み込む
Settings.Load(Path.Combine(RootDirectory, "settings.xml"));
Settings.Load(Path.Combine(RootDirectory, "settings.yml"));

// インストールされているいもうとを読み込む
CharacterManager.Rebuild(Path.Combine(RootDirectory, "characters"));
Expand All @@ -54,9 +53,9 @@ private void App_Startup(object sender, StartupEventArgs e)
// コンテキストを作成して、いもうとを起動
Context context = null;

if (Settings.Default.LastCharacter.HasValue)
if (Settings.Default.LastCharacter != null)
{
context = Context.Create(Settings.Default.LastCharacter.Value);
context = Context.Create(Settings.Default.LastCharacter);
}

context ??= Context.Create(_default) ?? Context.Create(CharacterManager.Characters.First().Key);
Expand All @@ -65,15 +64,15 @@ private void App_Startup(object sender, StartupEventArgs e)
}

private static readonly Mutex _mutex = new(false, "ImoutoDesktop");
private static readonly Guid _default = new("{F3EC60A3-C5FB-443a-B05E-C3345AB37269}");
private static readonly string _default = "sakura";

public string RootDirectory { get; private set; }

private void App_Exit(object sender, ExitEventArgs e)
{
if (!string.IsNullOrEmpty(RootDirectory))
{
Settings.Save(Path.Combine(RootDirectory, "settings.xml"));
Settings.Save(Path.Combine(RootDirectory, "settings.yml"));
}

_mutex.Close();
Expand Down
10 changes: 5 additions & 5 deletions src/ImoutoDesktop/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private Context(Character character)
RootDirectory = character.Directory;

// プロファイルを読み込む
Profile = Serializer<Profile>.Deserialize(Path.Combine(RootDirectory, "profile.xml"));
Profile = Profile.LoadFrom(Path.Combine(RootDirectory, "profile.yml"));
Profile.Age = Profile.Age == 0 ? Character.Age : Profile.Age;
Profile.TsundereLevel = Profile.TsundereLevel == 0 ? Character.TsundereLevel : Profile.TsundereLevel;

Expand Down Expand Up @@ -67,7 +67,7 @@ private Context(Character character)

private static readonly object _syncLock = new();

public static Context Create(Guid id)
public static Context Create(string id)
{
lock (_syncLock)
{
Expand Down Expand Up @@ -154,7 +154,7 @@ public void Shutdown()
// プロファイルを保存
Profile.LastBalloon = Balloon.Id;
Profile.BalloonOffset = BalloonWindow.LocationOffset;
Serializer<Profile>.Serialize(Path.Combine(RootDirectory, "profile.xml"), Profile);
Profile.SaveTo(Path.Combine(RootDirectory, "profile.yml"));
// コンテキストを削除
Delete(this);
}
Expand Down Expand Up @@ -277,7 +277,7 @@ private void InitializeScriptEngine()

private void CharacterCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
var id = (Guid)e.Parameter;
var id = (string)e.Parameter;
var context = Create(id);
if (context == null)
{
Expand All @@ -289,7 +289,7 @@ private void CharacterCommand_Executed(object sender, ExecutedRoutedEventArgs e)

private void BalloonCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
var id = (Guid)e.Parameter;
var id = (string)e.Parameter;

if (!BalloonManager.TryGetBalloon(id, out var balloon))
{
Expand Down
1 change: 1 addition & 0 deletions src/ImoutoDesktop/ImoutoDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="YamlDotNet" Version="11.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 15 additions & 11 deletions src/ImoutoDesktop/Models/Balloon.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
using System;
using System.IO;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace ImoutoDesktop.Models
{
[Serializable]
public class Balloon : PropertyChangedBase
public class Balloon
{
public Balloon()
{
ImoutoColor = "#000000";
UserColor = "#000000";
}

public Guid Id { get; set; }
public string Id { get; set; }

public string Name { get; set; }

public string ImoutoColor { get; set; }

public string UserColor { get; set; }

[XmlIgnore]
public bool CanSelect { get; set; }

[XmlIgnore]
public string Image => Path.Combine(Directory, "balloon.png");

[XmlIgnore]
public string ArrowUpImage => Path.Combine(Directory, "arrow0.png");

[XmlIgnore]
public string ArrowDownImage => Path.Combine(Directory, "arrow1.png");

[XmlIgnore]
public string Directory { get; set; }

public override int GetHashCode()
{
return Id.GetHashCode();
}

public static Balloon LoadFrom(string path)
{
using var reader = new StreamReader(path, Encoding.UTF8);

var balloon = Serializer.Deserialize<Balloon>(reader);

balloon.Directory = Path.GetDirectoryName(path);

return balloon;
}
}
}
22 changes: 9 additions & 13 deletions src/ImoutoDesktop/Models/BalloonManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;

Expand All @@ -9,14 +8,14 @@ public static class BalloonManager
{
public static string RootDirectory { get; private set; }

public static Dictionary<Guid, Balloon> Balloons { get; } = new();
public static Dictionary<string, Balloon> Balloons { get; } = new();

public static Balloon GetBalloon(Guid id)
public static Balloon GetBalloon(string id)
{
return Balloons.TryGetValue(id, out var balloon) ? balloon : Balloons.First().Value;
return id != null && Balloons.TryGetValue(id, out var balloon) ? balloon : Balloons.First().Value;
}

public static bool TryGetBalloon(Guid id, out Balloon balloon)
public static bool TryGetBalloon(string id, out Balloon balloon)
{
return Balloons.TryGetValue(id, out balloon);
}
Expand All @@ -32,20 +31,17 @@ public static void Rebuild(string searchDirectory)
// ディレクトリを辿る
foreach (var directory in Directory.GetDirectories(searchDirectory))
{
var path = Path.Combine(directory, "balloon.xml");
var path = Path.Combine(directory, "balloon.yml");

if (!File.Exists(path))
{
// 定義ファイルがないので無効
continue;
}

using (var stream = File.Open(path, FileMode.Open))
{
var balloon = Serializer<Balloon>.Deserialize(stream);
balloon.Directory = directory;
Balloons.Add(balloon.Id, balloon);
}
var balloon = Balloon.LoadFrom(path);

Balloons.Add(balloon.Id, balloon);
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions src/ImoutoDesktop/Models/Character.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
using System;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace ImoutoDesktop.Models
{
[Serializable]
public class Character : PropertyChangedBase
public class Character
{
public Character()
{
Age = 10;
TsundereLevel = 4;
}

public Guid Id { get; set; }
public string Id { get; set; }

public string Name { get; set; }

public string Description { get; set; }

public int Age { get; set; }

public int TsundereLevel { get; set; }

[XmlIgnore]
public bool CanSelect { get; set; }

[XmlIgnore]
public string Directory { get; set; }

public override int GetHashCode()
{
return Id.GetHashCode();
}

public static Character LoadFrom(string path)
{
using var reader = new StreamReader(path, Encoding.UTF8);

var character = Serializer.Deserialize<Character>(reader);

character.Directory = Path.GetDirectoryName(path);

return character;
}
}
}
18 changes: 7 additions & 11 deletions src/ImoutoDesktop/Models/CharacterManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;

namespace ImoutoDesktop.Models
Expand All @@ -8,9 +7,9 @@ public static class CharacterManager
{
public static string RootDirectory { get; private set; }

public static Dictionary<Guid, Character> Characters { get; } = new();
public static Dictionary<string, Character> Characters { get; } = new();

public static bool TryGetCharacter(Guid id, out Character character)
public static bool TryGetCharacter(string id, out Character character)
{
return Characters.TryGetValue(id, out character);
}
Expand All @@ -26,20 +25,17 @@ public static void Rebuild(string searchDirectory)
// ディレクトリを辿る
foreach (var directory in Directory.GetDirectories(searchDirectory))
{
var path = Path.Combine(directory, "character.xml");
var path = Path.Combine(directory, "character.yml");

if (!File.Exists(path))
{
// 定義ファイルがないので無効
continue;
}

using (var stream = File.Open(path, FileMode.Open))
{
var character = Serializer<Character>.Deserialize(stream);
character.Directory = directory;
Characters.Add(character.Id, character);
}
var character = Character.LoadFrom(path);

Characters.Add(character.Id, character);
}
}
}
Expand Down
Loading

0 comments on commit 5363eb9

Please sign in to comment.