using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using UnityEngine; using UnityEngine.EventSystems; namespace MyRoomCustom { public class CreativeRoomManager : MonoBehaviour { public CreativeRoom creativeRoom { get { return this.m_PtrCreativeRoom; } } public CreativeRoomUIObjectSettings creativeRoomUIObjSetting { get { return this.m_PtrCreativeRoomUIObjectSettings; } } public CreativeRoomUIPlacementMaid creativeRoomUIMaid { get { return this.m_PtrCreativeRoomUIPlacementMaid; } } public Camera inputCamera { get { return this.m_CamerauGUI; } } private float cameraFOV { set { if (value != this.m_CamerauGUI.fieldOfView) { this.m_CamerauGUI.fieldOfView = value; } } } private float cameraOrthographicSize { set { if (value != this.m_CamerauGUI.orthographicSize) { this.m_CamerauGUI.orthographicSize = value; } } } private bool cameraOrthographic { set { if (value != this.m_CamerauGUI.orthographic) { this.m_CamerauGUI.orthographic = value; } } } private Rect cameraRect { set { if (value != this.m_CamerauGUI.rect) { this.m_CamerauGUI.rect = value; } } } private void Awake() { Camera camera = GameMain.Instance.MainCamera.camera; this.m_MainCamera = camera; GameObject gameObject = new GameObject("[CreativeRoom]Camera (Input Module)"); this.m_CamerauGUI = gameObject.AddComponent(); this.m_CamerauGUI.clearFlags = CameraClearFlags.Nothing; this.m_CamerauGUI.useOcclusionCulling = false; this.m_CamerauGUI.enabled = false; this.m_CamerauGUI.allowHDR = false; this.m_CamerauGUI.allowMSAA = false; this.m_CamerauGUI.gameObject.AddComponent(); Transform transform = camera.transform; gameObject.transform.SetParent(transform, false); gameObject.transform.localPosition = Vector3.zero; gameObject.transform.localEulerAngles = Vector3.zero; gameObject.transform.localScale = Vector3.one; this.cameraFOV = camera.fieldOfView; this.cameraOrthographic = camera.orthographic; this.cameraOrthographicSize = camera.orthographicSize; this.cameraRect = camera.pixelRect; CharacterMgr characterMgr = GameMain.Instance.CharacterMgr; for (int i = 0; i < characterMgr.GetMaidCount(); i++) { if (characterMgr.GetMaid(i) != null) { characterMgr.GetMaid(i).Visible = false; } } } private void Start() { if (!this.m_PtrCreativeRoom) { this.m_PtrCreativeRoom = UnityEngine.Object.FindObjectOfType(); } if (!this.m_PtrCreativeRoomUIObjectSettings) { this.m_PtrCreativeRoomUIObjectSettings = UnityEngine.Object.FindObjectOfType(); } if (!this.m_PtrCreativeRoomUIPlacementMaid) { this.m_PtrCreativeRoomUIPlacementMaid = UnityEngine.Object.FindObjectOfType(); } this.GetTagBackup(); GameMain.Instance.BgMgr.DeleteBg(); GameMain.Instance.SoundMgr.PlayBGM(this.m_MusicName + ".ogg", 0.5f, true); GameMain.Instance.MainCamera.Reset(CameraMain.CameraType.Target, true); GameMain.Instance.MainCamera.SetTargetPos(new Vector3(0f, 1.8f, 0f), true); GameMain.Instance.MainCamera.SetDistance(7f, true); GameMain.Instance.MainCamera.SetAroundAngle(new Vector2(45f, 20f), true); GameMain.Instance.MainCamera.FadeIn(0.5f, false, null, true, true, default(Color)); if (!uGUITutorialPanel.IsOpened()) { uGUITutorialPanel.OpenTutorial("SceneCreativeRoom", null, false); } } private void Update() { GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID; if (vrdeviceTypeID != GameMain.VRDeviceType.RIFT_TOUCH && vrdeviceTypeID != GameMain.VRDeviceType.VIVE) { this.cameraFOV = this.m_MainCamera.fieldOfView; this.cameraOrthographic = this.m_MainCamera.orthographic; this.cameraOrthographicSize = this.m_MainCamera.orthographicSize; this.cameraRect = this.m_MainCamera.pixelRect; } } public CreativeRoomManager.CreativeRoomHeader Save(int number) { string text = UTY.gameProjectPath + "\\"; text += "MyRoom\\"; string text2 = "Room_"; text2 += number.ToString("000"); text2 += ".room"; if (!Directory.Exists(text)) { Directory.CreateDirectory(text); } MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); string text3 = DateTime.Now.ToString("yyyyMMddHHmm"); CreativeRoomManager.CreativeRoomHeader creativeRoomHeader = this.GetSaveDataHeader(number); if (creativeRoomHeader != null) { binaryWriter.Write("COM3D2_MY_ROOM"); binaryWriter.Write("2"); binaryWriter.Write(creativeRoomHeader.comment); binaryWriter.Write(text3); string value = creativeRoomHeader.guid; if (string.IsNullOrEmpty(value)) { value = Guid.NewGuid().ToString(); } binaryWriter.Write(value); creativeRoomHeader.lastWriteTime = text3; } else { binaryWriter.Write("COM3D2_MY_ROOM"); binaryWriter.Write("2"); binaryWriter.Write(string.Empty); binaryWriter.Write(text3); string text4 = Guid.NewGuid().ToString(); binaryWriter.Write(text4); creativeRoomHeader = new CreativeRoomManager.CreativeRoomHeader(); creativeRoomHeader.guid = text4; creativeRoomHeader.comment = string.Empty; creativeRoomHeader.lastWriteTime = text3; } this.m_PtrCreativeRoom.SavePanelData(binaryWriter); this.m_PtrCreativeRoomUIObjectSettings.SaveDeployObjectData(binaryWriter); File.WriteAllBytes(text + text2, memoryStream.ToArray()); memoryStream.Close(); memoryStream.Dispose(); return creativeRoomHeader; } public void Load(int number) { string text = UTY.gameProjectPath + "\\"; text += "MyRoom\\"; if (!Directory.Exists(text)) { Directory.CreateDirectory(text); } string text2 = text + "Room_" + number.ToString("000"); if (File.Exists(text2 + ".save")) { text2 += ".save"; } else { if (!File.Exists(text2 + ".room")) { Debug.LogWarning(string.Format("[CreativeRoomManager]{0}\u3000のセーブファイルが見つかりませんでした", text2)); return; } text2 += ".room"; } FileStream fileStream = new FileStream(text2, FileMode.Open); byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); fileStream.Close(); fileStream.Dispose(); BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer)); string a = binaryReader.ReadString(); int num = 0; if (a != "COM3D2_MY_ROOM") { binaryReader.BaseStream.Seek(0L, SeekOrigin.Begin); } else if (!int.TryParse(binaryReader.ReadString(), out num)) { string message = "[自室カスタム]セーブデータのヘッダーが不正です\n" + text2; Debug.LogError(message); NDebug.Assert(message, false); } binaryReader.ReadString(); binaryReader.ReadString(); if (num >= 1) { binaryReader.ReadString(); } this.m_PtrCreativeRoom.LoadPanelData(binaryReader); this.m_PtrCreativeRoomUIObjectSettings.LoadDeployObjectData(binaryReader); this.m_PtrCreativeRoomUIObjectSettings.SetDeploymentObjectGizmoEnable(false); binaryReader.Close(); } public CreativeRoomManager.CreativeRoomHeader GetSaveDataHeader(int number) { return this.GetSaveDataHeader(number.ToString("000")); } public CreativeRoomManager.CreativeRoomHeader GetSaveDataHeader(string number) { string str = UTY.gameProjectPath + "\\"; str += "MyRoom\\"; string text = "Room_" + number; if (File.Exists(str + text + ".save")) { text += ".save"; } else { text += ".room"; } if (!File.Exists(str + text)) { return null; } FileStream fileStream = new FileStream(str + text, FileMode.Open); byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); fileStream.Close(); fileStream.Dispose(); BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer)); CreativeRoomManager.CreativeRoomHeader creativeRoomHeader = new CreativeRoomManager.CreativeRoomHeader(); string a = binaryReader.ReadString(); int num = 0; if (a != "COM3D2_MY_ROOM") { binaryReader.BaseStream.Seek(0L, SeekOrigin.Begin); } else if (!int.TryParse(binaryReader.ReadString(), out num)) { string message = "[自室カスタム]セーブデータのヘッダーが不正です\n" + text; Debug.LogError(message); NDebug.Assert(message, false); } creativeRoomHeader.comment = binaryReader.ReadString(); creativeRoomHeader.lastWriteTime = binaryReader.ReadString(); if (num >= 1) { creativeRoomHeader.guid = binaryReader.ReadString(); } creativeRoomHeader.headerSize = binaryReader.BaseStream.Position; binaryReader.Close(); return creativeRoomHeader; } public void SaveComment(int number, string comment) { this.SaveComment(number.ToString("000"), comment); } public void SaveComment(string number, string comment) { CreativeRoomManager.CreativeRoomHeader saveDataHeader = this.GetSaveDataHeader(number); if (saveDataHeader == null) { return; } string str = UTY.gameProjectPath + "\\"; str += "MyRoom\\"; string path = str + "Room_" + number + ".room"; FileStream fileStream = new FileStream(path, FileMode.Open); byte[] array = new byte[fileStream.Length]; fileStream.Read(array, 0, (int)fileStream.Length); fileStream.Close(); fileStream.Dispose(); MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); int num = (int)saveDataHeader.headerSize; binaryWriter.Write("COM3D2_MY_ROOM"); binaryWriter.Write("2"); binaryWriter.Write(comment); binaryWriter.Write(saveDataHeader.lastWriteTime); if (string.IsNullOrEmpty(saveDataHeader.guid)) { saveDataHeader.guid = Guid.NewGuid().ToString(); } binaryWriter.Write(saveDataHeader.guid); binaryWriter.Write(array, num, array.Length - num); File.WriteAllBytes(path, memoryStream.ToArray()); memoryStream.Close(); memoryStream.Dispose(); Debug.Log("セーブデータ" + number + "番:ヘッダーを保存"); } public string DateTimeParse(string strDate) { return DateTime.ParseExact(strDate, "yyyyMMddHHmm", null).ToString("yyyy.MM.dd HH:mm"); } private void GetTagBackup() { Dictionary tag_backup = GameMain.Instance.ScriptMgr.adv_kag.tag_backup; string a; if (tag_backup != null && tag_backup.TryGetValue("name", out a) && a == "SceneCreativeRoom") { if (!tag_backup.TryGetValue("label", out this.m_strScriptReturnLabel)) { Debug.LogFormat("スクリプトからの引数「{0}」がありませんでした", new object[] { "label" }); } } } public void Exit() { if (!GameMain.Instance.SysDlg.IsDecided) { return; } GameMain.Instance.SysDlg.ShowFromLanguageTerm("ScenePhotoMode/セーブしていないデータは失われます。終了しますか?", null, SystemDialog.TYPE.YES_NO, delegate { if (string.IsNullOrEmpty(this.m_strScriptReturnLabel)) { Debug.Log("シーン終了時に飛ぶラベルがありませんでした"); return; } GameMain.Instance.MainCamera.FadeOut(0.5f, false, delegate { GameMain.Instance.SoundMgr.StopBGM(1f); PlacementData.Clear(); TextureData.Clear(); uGUIUtility.SetActiveEventSystem(true); ScriptManager scriptMgr = GameMain.Instance.ScriptMgr; scriptMgr.adv_kag.JumpLabel(this.m_strScriptReturnLabel); scriptMgr.adv_kag.Exec(); }, true, default(Color)); GameMain.Instance.SysDlg.Close(); }, delegate { GameMain.Instance.SysDlg.Close(); uGUIUtility.SetActiveEventSystem(true); }); uGUIUtility.SetActiveEventSystem(false); } private void OnDestroy() { if (this.m_CamerauGUI) { UnityEngine.Object.Destroy(this.m_CamerauGUI.gameObject); } } private static StringDictionary cachedSaveDataDic { get { if (CreativeRoomManager.m_CachedSaveDataDic == null) { CreativeRoomManager.m_CachedSaveDataDic = new StringDictionary(); string text = UTY.gameProjectPath + "\\"; text += "MyRoom\\"; if (Directory.Exists(text)) { string[] files = Directory.GetFiles(text, "*.room"); for (int i = 0; i < files.Length; i++) { CreativeRoomManager.CreativeRoomHeader creativeRoomHeader; CreativeRoomManager.DeserializeHeader(files[i], out creativeRoomHeader); CreativeRoomManager.m_CachedSaveDataDic[creativeRoomHeader.guid] = files[i]; } } } return CreativeRoomManager.m_CachedSaveDataDic; } } public static Dictionary GetSaveDataDic() { CreativeRoomManager.UpdateSaveDataGUID(); if (CreativeRoomManager.m_CachedSaveDataDic != null) { CreativeRoomManager.m_CachedSaveDataDic.Clear(); } CreativeRoomManager.m_CachedSaveDataDic = new StringDictionary(); string text = UTY.gameProjectPath + "\\"; text += "MyRoom\\"; Dictionary dictionary = null; if (Directory.Exists(text)) { dictionary = new Dictionary(); string[] files = Directory.GetFiles(text, "*.room"); for (int i = 0; i < files.Length; i++) { CreativeRoomManager.CreativeRoomHeader creativeRoomHeader; CreativeRoomManager.DeserializeHeader(files[i], out creativeRoomHeader); string text2 = string.Empty; if (string.IsNullOrEmpty(creativeRoomHeader.comment)) { text2 = "自室カスタム" + i.ToString("000"); } else { int num = 10; text2 = creativeRoomHeader.comment; if (text2.Length > num) { text2 = text2.Substring(0, num - 1) + "…"; } } dictionary.Add(creativeRoomHeader.guid, text2); CreativeRoomManager.m_CachedSaveDataDic[creativeRoomHeader.guid] = files[i]; } } return dictionary; } private static void UpdateSaveDataGUID() { string text = UTY.gameProjectPath + "\\"; text += "MyRoom\\"; if (Directory.Exists(text)) { SortedDictionary> sortedDictionary = new SortedDictionary>(); string[] files = Directory.GetFiles(text, "*.room"); for (int i = 0; i < files.Length; i++) { CreativeRoomManager.CreativeRoomHeader creativeRoomHeader; CreativeRoomManager.DeserializeHeader(files[i], out creativeRoomHeader); FileInfo fileInfo = new FileInfo(files[i]); if (sortedDictionary.ContainsKey(creativeRoomHeader.guid)) { string message = "ファイル「" + fileInfo.Name + "」の内部IDが重複しています。新しい内部IDを作成します。"; Debug.LogWarning(message); KeyValuePair keyValuePair = sortedDictionary[creativeRoomHeader.guid]; FileInfo key = keyValuePair.Key; CreativeRoomManager.CreativeRoomHeader value = keyValuePair.Value; CreativeRoomManager.CreativeRoomHeader creativeRoomHeader2 = (!(fileInfo.CreationTimeUtc <= key.CreationTimeUtc)) ? creativeRoomHeader : value; FileInfo info = (!(fileInfo.CreationTimeUtc <= key.CreationTimeUtc)) ? fileInfo : key; creativeRoomHeader2.guid = Guid.NewGuid().ToString(); CreativeRoomManager.SerializeAndUpdateHeader(info, creativeRoomHeader2); } if (!sortedDictionary.ContainsKey(creativeRoomHeader.guid)) { sortedDictionary.Add(creativeRoomHeader.guid, new KeyValuePair(fileInfo, creativeRoomHeader)); } else { KeyValuePair keyValuePair2 = sortedDictionary[creativeRoomHeader.guid]; sortedDictionary[creativeRoomHeader.guid] = new KeyValuePair(fileInfo, creativeRoomHeader); sortedDictionary.Add(keyValuePair2.Value.guid, new KeyValuePair(keyValuePair2.Key, keyValuePair2.Value)); } } } } public static GameObject InstantiateRoom(string guid_room) { if (!CreativeRoomManager.cachedSaveDataDic.ContainsKey(guid_room)) { string message = "[CreativeRoom]\u3000存在しないGUIDを読み込もうとしました"; NDebug.Assert(message, false); Debug.LogError(message); } GameObject gameObject = null; string path = CreativeRoomManager.cachedSaveDataDic[guid_room]; if (!File.Exists(path)) { string message2 = "[CreativeRoom]\u3000存在しないファイルを読み込もうとしました"; NDebug.Warning(message2); Debug.LogWarning(message2); } else { using (FileStream fileStream = new FileStream(path, FileMode.Open)) { byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer))) { CreativeRoomManager.CreativeRoomHeader creativeRoomHeader; CreativeRoomManager.DeserializeHeader(binaryReader, out creativeRoomHeader); long position = binaryReader.BaseStream.Position; int num = binaryReader.ReadInt32(); int num2 = binaryReader.ReadInt32(); int num3 = binaryReader.ReadInt32(); float num4 = binaryReader.ReadSingle(); binaryReader.BaseStream.Seek(position, SeekOrigin.Begin); GameObject gameObject2 = CreativeRoom.Load(binaryReader); GameObject gameObject3 = CreativeRoomUIObjectSettings.Load(binaryReader); gameObject2.name = "Parent Room Mesh"; gameObject3.name = "Parent Deployment Object"; gameObject = new GameObject("部屋でーた"); gameObject2.transform.SetParent(gameObject.transform, true); gameObject3.transform.SetParent(gameObject.transform, true); Vector3 localPosition = -0.5f * num4 * new Vector3((float)(num - 1), 0f, (float)(num3 - 1)); gameObject2.transform.localPosition = localPosition; gameObject3.transform.localPosition = localPosition; } } } return gameObject; } public static bool IsExistSaveData(string guid_room) { return CreativeRoomManager.cachedSaveDataDic.ContainsKey(guid_room) && File.Exists(CreativeRoomManager.cachedSaveDataDic[guid_room]); } private static void FileOpen(string path, Action action) { using (FileStream fileStream = new FileStream(path, FileMode.Open)) { byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer))) { action(binaryReader); } } } private static void DeserializeHeader(string path, out CreativeRoomManager.CreativeRoomHeader header) { using (FileStream fileStream = new FileStream(path, FileMode.Open)) { byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer))) { CreativeRoomManager.DeserializeHeader(binaryReader, out header); } } } private static void DeserializeHeader(BinaryReader br, out CreativeRoomManager.CreativeRoomHeader header) { string a = br.ReadString(); int num = 0; if (a != "COM3D2_MY_ROOM") { br.BaseStream.Seek(0L, SeekOrigin.Begin); } else if (!int.TryParse(br.ReadString(), out num)) { string message = "[CreativeRoom]\u3000セーブデータのヘッダーが不正です"; Debug.LogError(message); NDebug.Assert(message, false); } header = new CreativeRoomManager.CreativeRoomHeader(); header.comment = br.ReadString(); header.lastWriteTime = br.ReadString(); if (num >= 1) { header.guid = br.ReadString(); } header.headerSize = br.BaseStream.Position; } private static void SerializeAndUpdateHeader(FileInfo info, CreativeRoomManager.CreativeRoomHeader header) { CreativeRoomManager.CreativeRoomHeader creativeRoomHeader; CreativeRoomManager.DeserializeHeader(info.FullName, out creativeRoomHeader); FileStream fileStream = new FileStream(info.FullName, FileMode.Open); byte[] array = new byte[fileStream.Length]; fileStream.Read(array, 0, (int)fileStream.Length); fileStream.Close(); fileStream.Dispose(); MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); int num = (int)creativeRoomHeader.headerSize; binaryWriter.Write("COM3D2_MY_ROOM"); binaryWriter.Write("2"); binaryWriter.Write(header.comment); binaryWriter.Write(header.lastWriteTime); binaryWriter.Write(header.guid); binaryWriter.Write(array, num, array.Length - num); File.WriteAllBytes(info.FullName, memoryStream.ToArray()); memoryStream.Close(); memoryStream.Dispose(); Debug.Log("セーブデータ" + info.Name + ":ヘッダーを保存"); } [SerializeField] private CreativeRoom m_PtrCreativeRoom; [SerializeField] private CreativeRoomUIObjectSettings m_PtrCreativeRoomUIObjectSettings; [SerializeField] private CreativeRoomUIPlacementMaid m_PtrCreativeRoomUIPlacementMaid; [SerializeField] private RectTransform m_PtrCreativeRoomUIModeChangeButtons; private Camera m_MainCamera; private Camera m_CamerauGUI; private const string STR_SAVE_DATA_PATH = "MyRoom"; private const string STR_SAVE_FILE_NAME = "Room_"; private const string STR_SAVE_EXT = ".room"; private const string STR_SERIALIZE_VER = "2"; private const string STR_HEADER = "COM3D2_MY_ROOM"; public string m_MusicName = "BGM020"; private string m_strScriptReturnLabel = string.Empty; private static StringDictionary m_CachedSaveDataDic; private const string STR_ROOM_NAME_DEFAULT_HEADER = "自室カスタム"; private const int ROOM_NAME_MAX_COUNT = 10; private const string STR_ROOM_NAME_ELLIPSE = "…"; public class CreativeRoomHeader { public string guid; public string comment; public string lastWriteTime; public long headerSize; } } }