-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
インフェルノMODのパラメータをconfファイルで設定できるようにした #48
Changes from all commits
9a32a9e
9dba3e5
31d0f59
345ecd0
8223885
3a89e80
bac85b7
c68edd6
259477e
c5b8c5a
fb053b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,112 +3,34 @@ | |
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using Inferno.Utilities; | ||
using Newtonsoft.Json; | ||
|
||
namespace Inferno.ChaosMode | ||
{ | ||
/// <summary> | ||
/// カオスモード用設定ファイルのローダー | ||
/// </summary> | ||
public class ChaosModeSettingLoader | ||
public class ChaosModeSettingLoader : InfernoConfigLoader<ChaosModeSettingDTO> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
private readonly Encoding _encoding = Encoding.GetEncoding("Shift_JIS"); | ||
private DebugLogger _debugLogger; | ||
|
||
protected virtual DebugLogger ChaosModeDebugLogger | ||
{ | ||
get | ||
{ | ||
if (_debugLogger != null) return _debugLogger; | ||
_debugLogger = new DebugLogger(@"ChaosMod.log"); | ||
return _debugLogger; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// ファイルから読み込んで設定ファイルを生成する | ||
/// </summary> | ||
/// <param name="filePath">設定ファイルパス</param> | ||
/// <param name="fileName">設定ファイルパス</param> | ||
/// <returns>設定ファイル</returns> | ||
public ChaosModeSetting LoadSettingFile(string filePath) | ||
public new ChaosModeSetting LoadSettingFile(string fileName) | ||
{ | ||
//ファイルロード | ||
var readJson = ReadFile(filePath); | ||
try | ||
{ | ||
var dto = JsonConvert.DeserializeObject<ChaosModeSettingDTO>(readJson); | ||
return new ChaosModeSetting(dto); | ||
} | ||
catch (Exception e) | ||
{ | ||
ChaosModeDebugLogger.Log(e.Message); | ||
ChaosModeDebugLogger.Log(e.StackTrace); | ||
//例外発生時はデフォルトの設定ファイルを返す | ||
return new ChaosModeSetting(new ChaosModeSettingDTO()); | ||
} | ||
return new ChaosModeSetting(base.LoadSettingFile(fileName)); | ||
} | ||
|
||
/// <summary> | ||
/// ファイルから中身のstringを読み取る | ||
/// </summary> | ||
/// <param name="filePath">ファイルパス</param> | ||
/// <returns>結果</returns> | ||
protected virtual string ReadFile(string filePath) | ||
protected override ChaosModeSettingDTO CreateDefault() | ||
{ | ||
if (!File.Exists(filePath)) | ||
{ | ||
//存在しないならデフォルト設定ファイルを生成する | ||
CreateDefaultSettingFile(filePath); | ||
return ""; | ||
} | ||
var readString = ""; | ||
try | ||
{ | ||
using (var sr = new StreamReader(filePath, _encoding)) | ||
{ | ||
readString = sr.ReadToEnd(); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
ChaosModeDebugLogger.Log(e.Message); | ||
ChaosModeDebugLogger.Log(e.StackTrace); | ||
} | ||
return readString; | ||
} | ||
|
||
/// <summary> | ||
/// デフォルトの設定ファイルを生成する | ||
/// </summary> | ||
protected void CreateDefaultSettingFile(string filePath) | ||
{ | ||
var directoryPath = Path.GetDirectoryName(filePath); | ||
//存在しないならディレクトリを作る | ||
if (!Directory.Exists(directoryPath)) | ||
{ | ||
Directory.CreateDirectory(directoryPath); | ||
} | ||
|
||
//デフォルト設定を吐き出す | ||
var dto = new ChaosModeSettingDTO(); | ||
var chaosModeWeapons = new ChaosModeWeapons(); | ||
dto.WeaponList = chaosModeWeapons.ExcludeClosedWeapons.Select(x => x.ToString()).ToArray(); | ||
dto.WeaponListForDriveBy = chaosModeWeapons.DriveByWeapons.Select(x => x.ToString()).ToArray(); | ||
|
||
try | ||
{ | ||
using (var w = new StreamWriter(filePath, false, _encoding)) | ||
{ | ||
var json = JsonConvert.SerializeObject(dto, Formatting.Indented); | ||
w.WriteAsync(json); | ||
w.Flush(); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
ChaosModeDebugLogger.Log(e.Message); | ||
ChaosModeDebugLogger.Log(e.StackTrace); | ||
} | ||
return dto; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,38 @@ | |
using System.Collections.Generic; | ||
using System.Linq; | ||
using Inferno.ChaosMode; | ||
using Inferno.Utilities; | ||
using UniRx; | ||
|
||
namespace Inferno | ||
{ | ||
|
||
/// <summary> | ||
/// 市ニトロ | ||
/// </summary> | ||
public class CitizenNitro : InfernoScript | ||
{ | ||
class CitizenNitroConfig : InfernoConfig | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conf(Jsonファイル)に書き出したいパラメータを定義したConfigクラスを作る場合、InfernoConfigを継承させる |
||
{ | ||
public int Probability { get; set; } = 7; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここで指定するデフォルト値は、Jsonが存在しなかった時に書き込まれるデフォルトの値となる |
||
|
||
public override bool Validate() | ||
{ | ||
return Probability > 0 && Probability <= 100; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 値が正しいかどうかのチェック(バリデーション)をここに定義する |
||
} | ||
} | ||
|
||
protected override string ConfigFileName { get; } = "CitizenNitro.conf"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. confファイル名はここで指定 |
||
private CitizenNitroConfig config; | ||
private int Probability => config?.Probability ?? 7; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここで指定しているデフォルト値はConfigのLoadを忘れていた時にnullで落ちるのを防止するため。 |
||
|
||
private readonly string Keyword = "cnitro"; | ||
private readonly int probability = 7; | ||
private readonly int[] _velocities = { -100, -70, -50, 50, 70, 100 }; | ||
|
||
protected override void Setup() | ||
{ | ||
config = LoadConfig<CitizenNitroConfig>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setupの最初で指定したConfigをロードする |
||
|
||
//キーワードが入力されたらON/OFFを切り替える | ||
CreateInputKeywordAsObservable(Keyword) | ||
.Subscribe(_ => | ||
|
@@ -50,7 +67,7 @@ private void CitizenNitroAction() | |
|
||
foreach (var veh in nitroAvailableVeles) | ||
{ | ||
if (Random.Next(0, 100) <= probability) | ||
if (Random.Next(0, 100) <= Probability) | ||
{ | ||
StartCoroutine(DelayCoroutine(veh)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
難易度が下がりすぎていたのでついでに命中頻度を上げておく