Skip to content

Commit 0f1b2d2

Browse files
authored
Merge pull request #59 from TORISOUP/parupunte_name
パルプンテ発動時の字幕内容をconfに切り出した
2 parents 9023e23 + edd8090 commit 0f1b2d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+702
-517
lines changed

Inferno/Inferno.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,18 @@
114114
<Compile Include="InfernoScripts\InfernoCore\Drawer\TimerUITextManager.cs" />
115115
<Compile Include="InfernoScripts\InfernoCore\IsonoManager.cs" />
116116
<Compile Include="InfernoScripts\InfernoCore\Isono\IsonoTcpClient.cs" />
117+
<Compile Include="InfernoScripts\Parupunte\ParupunteConfigElement.cs" />
118+
<Compile Include="InfernoScripts\Parupunte\ParupunteConfigRepository.cs" />
117119
<Compile Include="InfernoScripts\Parupunte\ParupunteCore.cs" />
118120
<Compile Include="InfernoScripts\Parupunte\ParupunteScript.cs" />
119121
<Compile Include="InfernoScripts\Parupunte\Scripts\AddPlayerMoney.cs" />
120-
<Compile Include="InfernoScripts\Parupunte\Scripts\Avatar.cs" />
122+
<Compile Include="InfernoScripts\Parupunte\Scripts\Bunshin.cs" />
121123
<Compile Include="InfernoScripts\Parupunte\Scripts\BeastFriends.cs" />
122124
<Compile Include="InfernoScripts\Parupunte\Scripts\BlackOut.cs" />
123125
<Compile Include="InfernoScripts\Parupunte\Scripts\ChangeWantedLevel.cs" />
124126
<Compile Include="InfernoScripts\Parupunte\Scripts\ChangeWeather.cs" />
125127
<Compile Include="InfernoScripts\Parupunte\Scripts\CitizenEmagencyEscape.cs" />
126-
<Compile Include="InfernoScripts\Parupunte\Scripts\CitizenNinja.cs" />
128+
<Compile Include="InfernoScripts\Parupunte\Scripts\EdoPeriod.cs" />
127129
<Compile Include="InfernoScripts\Parupunte\Scripts\DachoClub.cs" />
128130
<Compile Include="InfernoScripts\Parupunte\Scripts\ElectricalShock.cs" />
129131
<Compile Include="InfernoScripts\Parupunte\Scripts\EveryoneLikeYou.cs" />
@@ -140,8 +142,8 @@
140142
<Compile Include="InfernoScripts\Parupunte\Scripts\RpgOnly.cs" />
141143
<Compile Include="InfernoScripts\Parupunte\Scripts\SpawnVheicle.cs" />
142144
<Compile Include="InfernoScripts\Parupunte\Scripts\Transform.cs" />
143-
<Compile Include="InfernoScripts\Parupunte\Scripts\VehicleSpeedUp.cs" />
144-
<Compile Include="InfernoScripts\Parupunte\Scripts\Gochiusa.cs" />
145+
<Compile Include="InfernoScripts\Parupunte\Scripts\VehicleEnginePowerUp.cs" />
146+
<Compile Include="InfernoScripts\Parupunte\Scripts\Tsukiji.cs" />
145147
<Compile Include="InfernoScripts\Parupunte\Scripts\InvisiblePeds.cs" />
146148
<Compile Include="InfernoScripts\Parupunte\Scripts\KillCitizens.cs" />
147149
<Compile Include="InfernoScripts\Parupunte\Scripts\Hitohanabi.cs" />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Inferno.InfernoScripts
10+
{
11+
internal class ParupunteConfigElement
12+
{
13+
public string StartMessage { get; set; }
14+
public string SubMessage { get; set; }
15+
public string FinishMessage { get; set; }
16+
17+
public ParupunteConfigElement(string startMessage, string subMessage, string finishMessage)
18+
{
19+
StartMessage = startMessage;
20+
SubMessage = subMessage;
21+
FinishMessage = finishMessage;
22+
}
23+
24+
public ParupunteConfigElement(string startMessage, string finishMessage)
25+
{
26+
StartMessage = startMessage;
27+
SubMessage = startMessage;
28+
FinishMessage = finishMessage;
29+
}
30+
31+
public static ParupunteConfigElement Default = new ParupunteConfigElement("", "");
32+
}
33+
34+
internal class ParupunteConfigDto
35+
{
36+
public string ParupunteName { get; set; }
37+
38+
[DefaultValue("")]
39+
public string StartMessage { get; set; }
40+
41+
[DefaultValue("")]
42+
public string SubMessage { get; set; }
43+
44+
[DefaultValue("")]
45+
public string FinishMessage { get; set; }
46+
47+
public ParupunteConfigDto(string parupunteName, string startMessage, string subMessage, string finishMessage)
48+
{
49+
ParupunteName = parupunteName;
50+
StartMessage = startMessage;
51+
SubMessage = subMessage;
52+
FinishMessage = finishMessage;
53+
}
54+
}
55+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Newtonsoft.Json;
8+
9+
namespace Inferno.InfernoScripts
10+
{
11+
/// <summary>
12+
/// ParupunteConfigElementの管理
13+
/// </summary>
14+
class ParupunteConfigRepository
15+
{
16+
protected readonly Encoding _encoding = Encoding.UTF8;
17+
protected DebugLogger _debugLogger;
18+
protected string _filePath = @"./scripts/confs/Parupunte.conf";
19+
20+
protected virtual DebugLogger DebugLogger
21+
{
22+
get
23+
{
24+
if (_debugLogger != null) return _debugLogger;
25+
_debugLogger = new DebugLogger(@"Inferno.log");
26+
return _debugLogger;
27+
}
28+
}
29+
30+
/// <summary>
31+
/// ファイルから読み込んで設定ファイル返す
32+
/// </summary>
33+
public Dictionary<string, ParupunteConfigElement> LoadSettingFile()
34+
{
35+
36+
if (!File.Exists(_filePath))
37+
{
38+
return new Dictionary<string, ParupunteConfigElement>();
39+
}
40+
41+
var readString = "";
42+
try
43+
{
44+
using (var sr = new StreamReader(_filePath, _encoding))
45+
{
46+
readString = sr.ReadToEnd();
47+
}
48+
}
49+
catch (Exception e)
50+
{
51+
DebugLogger.Log(e.Message);
52+
DebugLogger.Log(e.StackTrace);
53+
return new Dictionary<string, ParupunteConfigElement>();
54+
}
55+
56+
try
57+
{
58+
var dto = JsonConvert.DeserializeObject<ParupunteConfigDto[]>(readString);
59+
return dto.ToDictionary(x => x.ParupunteName,
60+
x => new ParupunteConfigElement(x.StartMessage, x.SubMessage, x.FinishMessage));
61+
}
62+
catch (Exception e)
63+
{
64+
DebugLogger.Log(e.Message);
65+
DebugLogger.Log(e.StackTrace);
66+
return new Dictionary<string, ParupunteConfigElement>();
67+
}
68+
}
69+
70+
public async Task SaveSettings(Dictionary<string, ParupunteConfigElement> configs)
71+
{
72+
var directoryPath = Path.GetDirectoryName(_filePath);
73+
//存在しないならディレクトリを作る
74+
if (!Directory.Exists(directoryPath))
75+
{
76+
Directory.CreateDirectory(directoryPath);
77+
}
78+
79+
var dto = configs.Select(x =>
80+
{
81+
var name = x.Key;
82+
var elem = x.Value;
83+
return new ParupunteConfigDto(name, elem.StartMessage, elem.SubMessage, elem.FinishMessage);
84+
}).ToArray();
85+
86+
try
87+
{
88+
using (var w = new StreamWriter(_filePath, false, _encoding))
89+
{
90+
var settings = new JsonSerializerSettings
91+
{
92+
NullValueHandling = NullValueHandling.Ignore
93+
};
94+
var json = JsonConvert.SerializeObject(dto, Formatting.Indented, settings);
95+
await w.WriteAsync(json);
96+
}
97+
}
98+
catch (Exception e)
99+
{
100+
DebugLogger.Log(e.Message);
101+
DebugLogger.Log(e.StackTrace);
102+
}
103+
}
104+
105+
}
106+
}

Inferno/InfernoScripts/Parupunte/ParupunteCore.cs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ internal class ParupunteCore : InfernoScript
2323
/// </summary>
2424
private Type[] _parupunteScritpts;
2525

26+
private Dictionary<string, ParupunteConfigElement> _parupunteConfigs;
27+
2628
/// <summary>
2729
/// デバッグ用
2830
/// </summary>
@@ -92,6 +94,12 @@ protected override void Setup()
9294

9395
#endregion ParunteScripts
9496

97+
#region Config
98+
99+
SetConfigData(_parupunteScritpts);
100+
101+
#endregion
102+
95103
#region EventHook
96104

97105
CreateInputKeywordAsObservable("rnt")
@@ -165,14 +173,54 @@ protected override void Setup()
165173

166174
}
167175

176+
// Configファイルの設定を行う
177+
private void SetConfigData(Type[] parupunteScripts)
178+
{
179+
var configRepository = new ParupunteConfigRepository();
180+
181+
// jsonから読み込んだConfig値
182+
var loadConfig = configRepository.LoadSettingFile();
183+
184+
// デフォルト値
185+
var defaultConfig = parupunteScripts
186+
.Select(x => new { x.Name, Attribute = x.GetCustomAttribute<ParupunteConfigAttribute>() })
187+
.Where(x => x.Attribute != null)
188+
.ToDictionary(x => x.Name, x =>
189+
{
190+
var a = x.Attribute;
191+
return new ParupunteConfigElement(a.DefaultStartMessage, a.DefaultSubMessage, a.DefaultEndMessage);
192+
});
193+
194+
// 合成したConfig値
195+
var mergedConfig = new Dictionary<string, ParupunteConfigElement>();
196+
197+
// デフォルト値のConfを、読み込んだConf値で上書きする
198+
foreach (var kv in defaultConfig)
199+
{
200+
var value = kv.Value;
201+
if (loadConfig.ContainsKey(kv.Key))
202+
{
203+
value = loadConfig[kv.Key];
204+
}
205+
mergedConfig[kv.Key] = value;
206+
}
207+
208+
// 最終的なconfをファイルに書き出す
209+
configRepository.SaveSettings(mergedConfig);
210+
211+
// 設定完了
212+
_parupunteConfigs = mergedConfig;
213+
}
214+
215+
168216
private bool IsonoMethod(string command)
169217
{
170218
var c = command;
171219

172220
if (c.Contains("とまれ"))
173221
{
174-
// ParupunteStop();
175-
// return true;
222+
// ParupunteStop();
223+
// return true;
176224
}
177225

178226
if (IsActive) return false;
@@ -202,8 +250,12 @@ private void ParupunteStart(Type script)
202250

203251
IsActive = true;
204252

253+
var conf = _parupunteConfigs.ContainsKey(script.Name)
254+
? _parupunteConfigs[script.Name]
255+
: ParupunteConfigElement.Default;
256+
205257
//ThreadPool上で初期化(プチフリ回避)
206-
Observable.Start(() => Activator.CreateInstance(script, this) as ParupunteScript, Scheduler.ThreadPool)
258+
Observable.Start(() => Activator.CreateInstance(script, this, conf) as ParupunteScript, Scheduler.ThreadPool)
207259
.OnErrorRetry((Exception ex) =>
208260
{
209261
LogWrite(ex.ToString());
@@ -256,6 +308,7 @@ private IEnumerable<object> ParupunteCoreCoroutine(ParupunteScript script)
256308
try
257309
{
258310
script.OnSetUp();
311+
script.OnSetNames();
259312
}
260313
catch (Exception e)
261314
{

Inferno/InfernoScripts/Parupunte/ParupunteScript.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Reflection;
45
using GTA;
56
using Inferno.InfernoScripts.Event;
67
using UniRx;
@@ -34,26 +35,40 @@ public ParupunteIsono(string command = null)
3435
Command = command;
3536
}
3637
}
37-
38+
39+
public class ParupunteConfigAttribute : Attribute
40+
{
41+
public string DefaultStartMessage = "";
42+
public string DefaultSubMessage = "";
43+
public string DefaultEndMessage = "";
44+
45+
public ParupunteConfigAttribute(string defaultStartMessage = "", string defaultEndMessage = "", string defaultSubMessage = "")
46+
{
47+
DefaultStartMessage = defaultStartMessage;
48+
DefaultEndMessage = defaultEndMessage;
49+
DefaultSubMessage = defaultSubMessage;
50+
}
51+
}
52+
3853
internal abstract class ParupunteScript
3954
{
4055
private bool IsFinished = false;
4156

4257
/// <summary>
4358
/// 開始時に表示されるメインメッセージ
4459
/// </summary>
45-
public abstract string Name { get; }
60+
public string Name { get; protected set; }
4661

4762
/// <summary>
4863
/// 画面右下に常に表示されるミニメッセージ
4964
/// </summary>
50-
public virtual string SubName { get; }
65+
public string SubName { get; protected set; }
5166

5267
/// <summary>
5368
/// 終了時に表示されるメッセージ
5469
/// nullまたは空文字列なら表示しない
5570
/// </summary>
56-
public virtual string EndMessage { get; }
71+
public Func<string> EndMessage { get; protected set; }
5772

5873
/// <summary>
5974
/// 終了メッセージの表示時間[s]
@@ -85,13 +100,28 @@ internal abstract class ParupunteScript
85100

86101
protected Random Random;
87102

88-
protected ParupunteScript(ParupunteCore core)
103+
protected readonly ParupunteConfigElement Config;
104+
105+
protected ParupunteScript(ParupunteCore core, ParupunteConfigElement element)
89106
{
90107
this.core = core;
91108
coroutineIds = new List<uint>();
92109
core.LogWrite(this.ToString());
93110
IsFinished = false;
94111
Random = new Random();
112+
113+
Config = element;
114+
115+
}
116+
117+
/// <summary>
118+
/// OnSetUpのあとに呼ばれる
119+
/// </summary>
120+
public virtual void OnSetNames()
121+
{
122+
Name = Config.StartMessage;
123+
SubName = Config.SubMessage;
124+
EndMessage = () => Config.FinishMessage;
95125
}
96126

97127
/// <summary>
@@ -143,9 +173,10 @@ public void OnFinishedCore()
143173
}
144174
coroutineIds.Clear();
145175

146-
if (!string.IsNullOrEmpty(EndMessage))
176+
var endMessage = EndMessage();
177+
if (!string.IsNullOrEmpty(endMessage))
147178
{
148-
core.DrawParupunteText(EndMessage, EndMessageDisplayTime);
179+
core.DrawParupunteText(endMessage, EndMessageDisplayTime);
149180
}
150181
}
151182

0 commit comments

Comments
 (0)