bool SaveFile(string file_path, BSIsoData iso) { if (!Directory.Exists(file_path)) { Directory.CreateDirectory(file_path); } file_path += iso.m_HeadInfo.Name + BuildingMan.s_IsoExt; try { using (FileStream fileStream = new FileStream(file_path, FileMode.Create, FileAccess.Write)) { BinaryWriter bw = new BinaryWriter(fileStream); byte[] datas = iso.Export(); bw.Write(datas); bw.Close(); } Debug.Log("Save building ISO successfully"); return(true); } catch (System.Exception) { // new PeTipMsg ("Failed to create file, please check the name", PeTipMsg.EMsgLevel.Error, PeTipMsg.EMsgType.Misc); return(false); } }
private static BSIsoData LoadISO(string full_path) { try { BSIsoData iso_data = new BSIsoData(); using (FileStream fs = new FileStream(full_path, FileMode.Open, FileAccess.Read)) { byte[] iso_buffer = new byte [(int)(fs.Length)]; fs.Read(iso_buffer, 0, (int)(fs.Length)); fs.Close(); if (iso_data.Import(iso_buffer)) { return(iso_data); } else { return(null); } } } catch (Exception e) { Debug.LogError("Loading ISO Error : " + e.ToString()); return(null); } }
/// <summary> /// Extracts the all the headers of building isos. /// </summary> /// <returns>The the headers.</returns> public BSIsoHeadData[] ExtractTheHeaders() { string FilePath = GameConfig.GetUserDataPath() + BuildingMan.s_IsoPath; if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); } string[] fileNames = Directory.GetFiles(FilePath); List <BSIsoHeadData> headers = new List <BSIsoHeadData>(); foreach (string fn in fileNames) { if (fn.Contains(".biso")) { BSIsoHeadData h; BSIsoData.ExtractHeader(fn, out h); int start = fn.LastIndexOf('/') + 1; int end = fn.LastIndexOf('.'); h.Name = fn.Substring(start, end - start); headers.Add(h); } } return(headers.ToArray()); }
public static BIsoCursor CreateIsoCursor(string full_path, int layer = 0) { GameObject res_go = Resources.Load(s_PrefabPath) as GameObject; if (res_go == null) { throw new Exception("Load iso cursor prefab failed"); } BIsoCursor res_cursor = res_go.GetComponent <BIsoCursor>(); if (res_cursor == null) { throw new Exception("Load iso cursor prefab failed"); } BIsoCursor iso_cursor = BIsoCursor.Instantiate(res_cursor) as BIsoCursor; BSIsoData iso = LoadISO(full_path); iso_cursor.ISO = iso; if (iso.m_HeadInfo.Mode == EBSVoxelType.Block) { Vector3 size = new Vector3(iso.m_HeadInfo.xSize, iso.m_HeadInfo.ySize, iso.m_HeadInfo.zSize); iso_cursor.SetBoundSizeOfBlock(size, iso_cursor.gameObject); iso_cursor.Computer = iso_cursor.BlockGroup.gameObject.AddComponent <BSB45Computer>(); foreach (KeyValuePair <int, BSVoxel> kvp in iso_cursor.ISO.m_Voxels) { IntVector3 index = BSIsoData.KeyToIPos(kvp.Key); iso_cursor.Computer.AlterBlockInBuild(index.x, index.y, index.z, kvp.Value.ToBlock()); } iso_cursor.Computer.RebuildMesh(); } else if (iso.m_HeadInfo.Mode == EBSVoxelType.Voxel) { Debug.LogError("Cant Support the iso voxel"); Destroy(res_go); Destroy(iso_cursor.gameObject); return(null); } Transform[] ts = iso_cursor.gameObject.GetComponentsInChildren <Transform>(true); foreach (Transform t in ts) { t.gameObject.layer = layer; } iso_cursor.gameObject.SetActive(true); iso_cursor.BlockGroup.gameObject.SetActive(true); iso_cursor.Bound.gameObject.SetActive(true); iso_cursor.testHead = iso.m_HeadInfo; return(iso_cursor); }
//void OnMenuQuickSwitchPointBrush() //{ // if (pointMode == EBSBrushMode.Add) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.pointRemove); // else if (pointMode == EBSBrushMode.Subtract) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.pointAdd); //} //void OnMenuQuickSwitchDiagonalBrush() //{ // if (diagonalRot == 0) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.diagonalZPos); // else if (diagonalRot == 1) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.diagonalXNeg); // else if (diagonalRot == 2) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.diagonalZNeg); // else if (diagonalRot == 3) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.diagonalXPos); //} //void OnMenuQuickSwitchBoxBrush() //{ // if (boxMode == EBSBrushMode.Add) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.boxRemove); // else if (boxMode == EBSBrushMode.Subtract) // mMenuCtrl.OnBrushItemClick(UIBrushMenuItem.BrushType.boxAdd); //} #endregion #endregion #region Save_Wnd_Event bool OnSaveIsoClick(string iso_name) { BSMiscBrush select_brush = m_CurBrush as BSMiscBrush; BSIsoSelectBrush iso_select = m_CurBrush as BSIsoSelectBrush; if (select_brush != null || iso_select != null) { if (iso_name != "") { // Icon int width = PEBuildingMan.Self.IsoCaputure.photoRT.width; int height = PEBuildingMan.Self.IsoCaputure.photoRT.height; Texture2D iconTex = new Texture2D(width, height, TextureFormat.ARGB32, false); RenderTexture.active = PEBuildingMan.Self.IsoCaputure.photoRT; iconTex.ReadPixels(new Rect(0, 0, width, height), 0, 0); iconTex.Apply(); RenderTexture.active = null; BSIsoData iso = null; if (select_brush != null && select_brush.SaveToIso(iso_name, iconTex.EncodeToPNG(), out iso)) { _initIosBlockPage = false; InitIsoBlockPage(); if (onSaveIsoClick != null) { onSaveIsoClick(); } return(true); } else if (iso_select != null && iso_select.SaveToIso(iso_name, iconTex.EncodeToPNG(), out iso)) { _initIosBlockPage = false; InitIsoBlockPage(); if (onSaveIsoClick != null) { onSaveIsoClick(); } return(true); } else { MessageBox_N.ShowOkBox(PELocalization.GetString(8000494)); } } else { MessageBox_N.ShowOkBox(PELocalization.GetString(8000494)); } } return(false); }
void SaveFile(string file_path, BSIsoData iso) { if (!Directory.Exists(file_path)) { Directory.CreateDirectory(file_path); } file_path += iso.m_HeadInfo.Name + s_Ext; using (FileStream fileStream = new FileStream(file_path, FileMode.Create, FileAccess.Write)) { BinaryWriter bw = new BinaryWriter(fileStream); byte[] datas = iso.Export(); bw.Write(datas); bw.Close(); } }
private void OnOutputBlocks(Dictionary <int, BSVoxel> voxels, Vector3 originalPos) { List <IntVector3> posLst = new List <IntVector3>(); List <B45Block> blockLst = new List <B45Block>(); foreach (KeyValuePair <int, BSVoxel> kvp in voxels) { posLst.Add(BSIsoData.KeyToIPos(kvp.Key)); blockLst.Add(kvp.Value.ToBlock()); } B45Block.RepositionBlocks(posLst, blockLst, m_Rot, originalPos); int n = posLst.Count; for (int i = 0; i < n; i++) { IntVector3 inpos = posLst[i]; m_Indexes.Add(inpos); m_OldVoxel.Add(BuildingMan.Blocks.SafeRead(inpos.x, inpos.y, inpos.z)); m_NewVoxel.Add(new BSVoxel(blockLst[i])); m_VoxelMap[inpos] = 0; } }
//const float intervalTime = 0.2f; //private float _curTime = 0; //private bool _down = false; protected override bool AfterSelectionUpdate() { if (!m_Selecting) { if (m_SelectionBoxes.Count == 0) { return(true); } if (!_extruding) { // Do delelete voxel if (BSInput.s_Delete) { DeleteVoxel(); } // Do Save else if (Input.GetKeyDown(KeyCode.Period)) { BSIsoData iso = null; SaveToIso(IsoSavedName, new byte[0], out iso); } } if (!_extruding) { // Do ExtrudeSelection if (BSInput.s_Shift && BSInput.s_Left) { ExtrudeSelection(-1, 0, 0); _startExtrudeTime = 0; } if (BSInput.s_Shift && BSInput.s_Right) { ExtrudeSelection(1, 0, 0); _startExtrudeTime = 0; } if (BSInput.s_Shift && BSInput.s_Up) { ExtrudeSelection(0, 1, 0); _startExtrudeTime = 0; } if (BSInput.s_Shift && BSInput.s_Down) { ExtrudeSelection(0, -1, 0); _startExtrudeTime = 0; } if (BSInput.s_Shift && BSInput.s_Forward) { ExtrudeSelection(0, 0, 1); _startExtrudeTime = 0; } if (BSInput.s_Shift && BSInput.s_Back) { ExtrudeSelection(0, 0, -1); _startExtrudeTime = 0; } } if (_extruding) { if (_startExtrudeTime > 0.3f) { _startExtrudeTime = 0; _extruding = false; return(true); } else { _startExtrudeTime += Time.deltaTime; return(false); } } } return(true); }
public bool SaveToIso(string IsoName, byte[] icon_tex, out BSIsoData outData) { outData = null; if (m_SelectionBoxes.Count == 0) { return(false); } // Only the block can save to be ISO if (pattern.type != EBSVoxelType.Block) { Debug.LogWarning("The iso is not support the Voxel"); return(false); } BSIsoData iso = new BSIsoData(); iso.Init(pattern.type); iso.m_HeadInfo.Name = IsoName; BSTools.IntBox bound = BSTools.SelBox.CalculateBound(m_SelectionBoxes); iso.m_HeadInfo.xSize = bound.xMax - bound.xMin + 1; iso.m_HeadInfo.ySize = bound.yMax - bound.yMin + 1; iso.m_HeadInfo.zSize = bound.zMax - bound.zMin + 1; iso.m_HeadInfo.IconTex = icon_tex; foreach (BSTools.SelBox box in m_SelectionBoxes) { for (int x = box.m_Box.xMin; x <= box.m_Box.xMax; x++) { for (int y = box.m_Box.yMin; y <= box.m_Box.yMax; y++) { for (int z = box.m_Box.zMin; z <= box.m_Box.zMax; z++) { BSVoxel voxel = dataSource.SafeRead(x, y, z); int key = BSIsoData.IPosToKey(x - bound.xMin, y - bound.yMin, z - bound.zMin); if (!dataSource.VoxelIsZero(voxel, 1)) { iso.m_Voxels.Add(key, voxel); } } } } } if (iso.m_Voxels.Count == 0) { return(false); } iso.CaclCosts(); string FilePath = GameConfig.GetUserDataPath() + BuildingMan.s_IsoPath; /*bool r = */ SaveFile(FilePath, iso); if (SaveFile(FilePath, iso)) { ClearSelection(m_Action); outData = iso; return(true); } else { return(false); } }
public bool SaveToIso(string IsoName, byte[] icon_tex, out BSIsoData outData) { outData = null; List <IntVector3> selections = GetSelectionPos(); if (selections.Count == 0) { return(false); } // Only the block can save to be ISO if (pattern.type != EBSVoxelType.Block) { Debug.LogWarning("The iso is not support the Voxel"); return(false); } BSIsoData iso = new BSIsoData(); iso.Init(pattern.type); iso.m_HeadInfo.Name = IsoName; IntVector3 min = new IntVector3(selections[0]); IntVector3 max = new IntVector3(selections[0]); for (int i = 1; i < selections.Count; i++) { min.x = (min.x > selections[i].x ? selections[i].x:min.x); min.y = (min.y > selections[i].y ? selections[i].y:min.y); min.z = (min.z > selections[i].z ? selections[i].z:min.z); max.x = (max.x < selections[i].x ? selections[i].x:max.x); max.y = (max.y < selections[i].y ? selections[i].y:max.y); max.z = (max.z < selections[i].z ? selections[i].z:max.z); } iso.m_HeadInfo.xSize = max.x - min.x + 1; iso.m_HeadInfo.ySize = max.y - min.y + 1; iso.m_HeadInfo.zSize = max.z - min.z + 1; iso.m_HeadInfo.IconTex = icon_tex; for (int i = 0; i < selections.Count; i++) { BSVoxel voxel = dataSource.SafeRead(selections[i].x, selections[i].y, selections[i].z); int key = BSIsoData.IPosToKey(selections[i].x - min.x, selections[i].y - min.y, selections[i].z - min.z); iso.m_Voxels.Add(key, voxel); } iso.CaclCosts(); string FilePath = GameConfig.GetUserDataPath() + BuildingMan.s_IsoPath; /*bool r = */ SaveFile(FilePath, iso); if (SaveFile(FilePath, iso)) { outData = iso; return(true); } else { return(false); } }