Skip to content

Commit

Permalink
Merge pull request #59 from TORISOUP/parupunte_name
Browse files Browse the repository at this point in the history
パルプンテ発動時の字幕内容をconfに切り出した
  • Loading branch information
TORISOUP authored Apr 14, 2018
2 parents 9023e23 + edd8090 commit 0f1b2d2
Show file tree
Hide file tree
Showing 57 changed files with 702 additions and 517 deletions.
10 changes: 6 additions & 4 deletions Inferno/Inferno.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,18 @@
<Compile Include="InfernoScripts\InfernoCore\Drawer\TimerUITextManager.cs" />
<Compile Include="InfernoScripts\InfernoCore\IsonoManager.cs" />
<Compile Include="InfernoScripts\InfernoCore\Isono\IsonoTcpClient.cs" />
<Compile Include="InfernoScripts\Parupunte\ParupunteConfigElement.cs" />
<Compile Include="InfernoScripts\Parupunte\ParupunteConfigRepository.cs" />
<Compile Include="InfernoScripts\Parupunte\ParupunteCore.cs" />
<Compile Include="InfernoScripts\Parupunte\ParupunteScript.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\AddPlayerMoney.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\Avatar.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\Bunshin.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\BeastFriends.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\BlackOut.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\ChangeWantedLevel.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\ChangeWeather.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\CitizenEmagencyEscape.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\CitizenNinja.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\EdoPeriod.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\DachoClub.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\ElectricalShock.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\EveryoneLikeYou.cs" />
Expand All @@ -140,8 +142,8 @@
<Compile Include="InfernoScripts\Parupunte\Scripts\RpgOnly.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\SpawnVheicle.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\Transform.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\VehicleSpeedUp.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\Gochiusa.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\VehicleEnginePowerUp.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\Tsukiji.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\InvisiblePeds.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\KillCitizens.cs" />
<Compile Include="InfernoScripts\Parupunte\Scripts\Hitohanabi.cs" />
Expand Down
55 changes: 55 additions & 0 deletions Inferno/InfernoScripts/Parupunte/ParupunteConfigElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inferno.InfernoScripts
{
internal class ParupunteConfigElement
{
public string StartMessage { get; set; }
public string SubMessage { get; set; }
public string FinishMessage { get; set; }

public ParupunteConfigElement(string startMessage, string subMessage, string finishMessage)
{
StartMessage = startMessage;
SubMessage = subMessage;
FinishMessage = finishMessage;
}

public ParupunteConfigElement(string startMessage, string finishMessage)
{
StartMessage = startMessage;
SubMessage = startMessage;
FinishMessage = finishMessage;
}

public static ParupunteConfigElement Default = new ParupunteConfigElement("", "");
}

internal class ParupunteConfigDto
{
public string ParupunteName { get; set; }

[DefaultValue("")]
public string StartMessage { get; set; }

[DefaultValue("")]
public string SubMessage { get; set; }

[DefaultValue("")]
public string FinishMessage { get; set; }

public ParupunteConfigDto(string parupunteName, string startMessage, string subMessage, string finishMessage)
{
ParupunteName = parupunteName;
StartMessage = startMessage;
SubMessage = subMessage;
FinishMessage = finishMessage;
}
}
}
106 changes: 106 additions & 0 deletions Inferno/InfernoScripts/Parupunte/ParupunteConfigRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Inferno.InfernoScripts
{
/// <summary>
/// ParupunteConfigElementの管理
/// </summary>
class ParupunteConfigRepository
{
protected readonly Encoding _encoding = Encoding.UTF8;
protected DebugLogger _debugLogger;
protected string _filePath = @"./scripts/confs/Parupunte.conf";

protected virtual DebugLogger DebugLogger
{
get
{
if (_debugLogger != null) return _debugLogger;
_debugLogger = new DebugLogger(@"Inferno.log");
return _debugLogger;
}
}

/// <summary>
/// ファイルから読み込んで設定ファイル返す
/// </summary>
public Dictionary<string, ParupunteConfigElement> LoadSettingFile()
{

if (!File.Exists(_filePath))
{
return new Dictionary<string, ParupunteConfigElement>();
}

var readString = "";
try
{
using (var sr = new StreamReader(_filePath, _encoding))
{
readString = sr.ReadToEnd();
}
}
catch (Exception e)
{
DebugLogger.Log(e.Message);
DebugLogger.Log(e.StackTrace);
return new Dictionary<string, ParupunteConfigElement>();
}

try
{
var dto = JsonConvert.DeserializeObject<ParupunteConfigDto[]>(readString);
return dto.ToDictionary(x => x.ParupunteName,
x => new ParupunteConfigElement(x.StartMessage, x.SubMessage, x.FinishMessage));
}
catch (Exception e)
{
DebugLogger.Log(e.Message);
DebugLogger.Log(e.StackTrace);
return new Dictionary<string, ParupunteConfigElement>();
}
}

public async Task SaveSettings(Dictionary<string, ParupunteConfigElement> configs)
{
var directoryPath = Path.GetDirectoryName(_filePath);
//存在しないならディレクトリを作る
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

var dto = configs.Select(x =>
{
var name = x.Key;
var elem = x.Value;
return new ParupunteConfigDto(name, elem.StartMessage, elem.SubMessage, elem.FinishMessage);
}).ToArray();

try
{
using (var w = new StreamWriter(_filePath, false, _encoding))
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
var json = JsonConvert.SerializeObject(dto, Formatting.Indented, settings);
await w.WriteAsync(json);
}
}
catch (Exception e)
{
DebugLogger.Log(e.Message);
DebugLogger.Log(e.StackTrace);
}
}

}
}
59 changes: 56 additions & 3 deletions Inferno/InfernoScripts/Parupunte/ParupunteCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal class ParupunteCore : InfernoScript
/// </summary>
private Type[] _parupunteScritpts;

private Dictionary<string, ParupunteConfigElement> _parupunteConfigs;

/// <summary>
/// デバッグ用
/// </summary>
Expand Down Expand Up @@ -92,6 +94,12 @@ protected override void Setup()

#endregion ParunteScripts

#region Config

SetConfigData(_parupunteScritpts);

#endregion

#region EventHook

CreateInputKeywordAsObservable("rnt")
Expand Down Expand Up @@ -165,14 +173,54 @@ protected override void Setup()

}

// Configファイルの設定を行う
private void SetConfigData(Type[] parupunteScripts)
{
var configRepository = new ParupunteConfigRepository();

// jsonから読み込んだConfig値
var loadConfig = configRepository.LoadSettingFile();

// デフォルト値
var defaultConfig = parupunteScripts
.Select(x => new { x.Name, Attribute = x.GetCustomAttribute<ParupunteConfigAttribute>() })
.Where(x => x.Attribute != null)
.ToDictionary(x => x.Name, x =>
{
var a = x.Attribute;
return new ParupunteConfigElement(a.DefaultStartMessage, a.DefaultSubMessage, a.DefaultEndMessage);
});

// 合成したConfig値
var mergedConfig = new Dictionary<string, ParupunteConfigElement>();

// デフォルト値のConfを、読み込んだConf値で上書きする
foreach (var kv in defaultConfig)
{
var value = kv.Value;
if (loadConfig.ContainsKey(kv.Key))
{
value = loadConfig[kv.Key];
}
mergedConfig[kv.Key] = value;
}

// 最終的なconfをファイルに書き出す
configRepository.SaveSettings(mergedConfig);

// 設定完了
_parupunteConfigs = mergedConfig;
}


private bool IsonoMethod(string command)
{
var c = command;

if (c.Contains("とまれ"))
{
// ParupunteStop();
// return true;
// ParupunteStop();
// return true;
}

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

IsActive = true;

var conf = _parupunteConfigs.ContainsKey(script.Name)
? _parupunteConfigs[script.Name]
: ParupunteConfigElement.Default;

//ThreadPool上で初期化(プチフリ回避)
Observable.Start(() => Activator.CreateInstance(script, this) as ParupunteScript, Scheduler.ThreadPool)
Observable.Start(() => Activator.CreateInstance(script, this, conf) as ParupunteScript, Scheduler.ThreadPool)
.OnErrorRetry((Exception ex) =>
{
LogWrite(ex.ToString());
Expand Down Expand Up @@ -256,6 +308,7 @@ private IEnumerable<object> ParupunteCoreCoroutine(ParupunteScript script)
try
{
script.OnSetUp();
script.OnSetNames();
}
catch (Exception e)
{
Expand Down
45 changes: 38 additions & 7 deletions Inferno/InfernoScripts/Parupunte/ParupunteScript.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using GTA;
using Inferno.InfernoScripts.Event;
using UniRx;
Expand Down Expand Up @@ -34,26 +35,40 @@ public ParupunteIsono(string command = null)
Command = command;
}
}


public class ParupunteConfigAttribute : Attribute
{
public string DefaultStartMessage = "";
public string DefaultSubMessage = "";
public string DefaultEndMessage = "";

public ParupunteConfigAttribute(string defaultStartMessage = "", string defaultEndMessage = "", string defaultSubMessage = "")
{
DefaultStartMessage = defaultStartMessage;
DefaultEndMessage = defaultEndMessage;
DefaultSubMessage = defaultSubMessage;
}
}

internal abstract class ParupunteScript
{
private bool IsFinished = false;

/// <summary>
/// 開始時に表示されるメインメッセージ
/// </summary>
public abstract string Name { get; }
public string Name { get; protected set; }

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

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

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

protected Random Random;

protected ParupunteScript(ParupunteCore core)
protected readonly ParupunteConfigElement Config;

protected ParupunteScript(ParupunteCore core, ParupunteConfigElement element)
{
this.core = core;
coroutineIds = new List<uint>();
core.LogWrite(this.ToString());
IsFinished = false;
Random = new Random();

Config = element;

}

/// <summary>
/// OnSetUpのあとに呼ばれる
/// </summary>
public virtual void OnSetNames()
{
Name = Config.StartMessage;
SubName = Config.SubMessage;
EndMessage = () => Config.FinishMessage;
}

/// <summary>
Expand Down Expand Up @@ -143,9 +173,10 @@ public void OnFinishedCore()
}
coroutineIds.Clear();

if (!string.IsNullOrEmpty(EndMessage))
var endMessage = EndMessage();
if (!string.IsNullOrEmpty(endMessage))
{
core.DrawParupunteText(EndMessage, EndMessageDisplayTime);
core.DrawParupunteText(endMessage, EndMessageDisplayTime);
}
}

Expand Down
Loading

0 comments on commit 0f1b2d2

Please sign in to comment.