Port LoadLevelScreen to C

This commit is contained in:
UnknownShadow200 2018-04-11 11:49:10 +10:00
parent 58585827fc
commit 6bc8ea5385
10 changed files with 121 additions and 43 deletions

View File

@ -73,7 +73,7 @@ namespace ClassicalSharp.Gui.Screens {
game.World.SetNewMap(blocks, width, height, length); game.World.SetNewMap(blocks, width, height, length);
game.WorldEvents.RaiseOnNewMapLoaded(); game.WorldEvents.RaiseOnNewMapLoaded();
if (game.UseServerTextures && game.World.TextureUrl != null) if (game.AllowServerTextures && game.World.TextureUrl != null)
game.Server.RetrieveTexturePack(game.World.TextureUrl); game.Server.RetrieveTexturePack(game.World.TextureUrl);
LocalPlayer p = game.LocalPlayer; LocalPlayer p = game.LocalPlayer;

View File

@ -54,14 +54,14 @@ namespace ClassicalSharp.Gui.Screens {
static string GetOpts(Game g) { return GetBool(g.UseClassicOptions); } static string GetOpts(Game g) { return GetBool(g.UseClassicOptions); }
static void SetOpts(Game g, string v) { g.UseClassicOptions = SetBool(v, OptionsKey.UseClassicOptions); } static void SetOpts(Game g, string v) { g.UseClassicOptions = SetBool(v, OptionsKey.UseClassicOptions); }
static string GetCustom(Game g) { return GetBool(g.UseCustomBlocks); } static string GetCustom(Game g) { return GetBool(g.AllowCustomBlocks); }
static void SetCustom(Game g, string v) { g.UseCustomBlocks = SetBool(v, OptionsKey.UseCustomBlocks); } static void SetCustom(Game g, string v) { g.AllowCustomBlocks = SetBool(v, OptionsKey.UseCustomBlocks); }
static string GetCPE(Game g) { return GetBool(g.UseCPE); } static string GetCPE(Game g) { return GetBool(g.UseCPE); }
static void SetCPE(Game g, string v) { g.UseCPE = SetBool(v, OptionsKey.UseCPE); } static void SetCPE(Game g, string v) { g.UseCPE = SetBool(v, OptionsKey.UseCPE); }
static string GetTexs(Game g) { return GetBool(g.UseServerTextures); } static string GetTexs(Game g) { return GetBool(g.AllowServerTextures); }
static void SetTexs(Game g, string v) { g.UseServerTextures = SetBool(v, OptionsKey.UseServerTextures); } static void SetTexs(Game g, string v) { g.AllowServerTextures = SetBool(v, OptionsKey.UseServerTextures); }
static void SwitchBack(Game g, Widget w) { static void SwitchBack(Game g, Widget w) {
if (g.UseClassicOptions) { if (g.UseClassicOptions) {

View File

@ -169,7 +169,7 @@ namespace ClassicalSharp {
void LoadOptions() { void LoadOptions() {
ClassicMode = Options.GetBool("mode-classic", false); ClassicMode = Options.GetBool("mode-classic", false);
ClassicHacks = Options.GetBool(OptionsKey.AllowClassicHacks, false); ClassicHacks = Options.GetBool(OptionsKey.AllowClassicHacks, false);
UseCustomBlocks = Options.GetBool(OptionsKey.UseCustomBlocks, true); AllowCustomBlocks = Options.GetBool(OptionsKey.UseCustomBlocks, true);
UseCPE = Options.GetBool(OptionsKey.UseCPE, true); UseCPE = Options.GetBool(OptionsKey.UseCPE, true);
SimpleArmsAnim = Options.GetBool(OptionsKey.SimpleArmsAnim, false); SimpleArmsAnim = Options.GetBool(OptionsKey.SimpleArmsAnim, false);
ChatLogging = Options.GetBool(OptionsKey.ChatLogging, true); ChatLogging = Options.GetBool(OptionsKey.ChatLogging, true);
@ -189,7 +189,7 @@ namespace ClassicalSharp {
CameraClipping = Options.GetBool(OptionsKey.CameraClipping, true); CameraClipping = Options.GetBool(OptionsKey.CameraClipping, true);
MaxChunkUpdates = Options.GetInt(OptionsKey.MaxChunkUpdates, 4, 1024, 30); MaxChunkUpdates = Options.GetInt(OptionsKey.MaxChunkUpdates, 4, 1024, 30);
UseServerTextures = Options.GetBool(OptionsKey.UseServerTextures, true); AllowServerTextures = Options.GetBool(OptionsKey.UseServerTextures, true);
MouseSensitivity = Options.GetInt(OptionsKey.Sensitivity, 1, 100, 30); MouseSensitivity = Options.GetInt(OptionsKey.Sensitivity, 1, 100, 30);
ShowBlockInHand = Options.GetBool(OptionsKey.ShowBlockInHand, true); ShowBlockInHand = Options.GetBool(OptionsKey.ShowBlockInHand, true);
InvertMouse = Options.GetBool(OptionsKey.InvertMouse, false); InvertMouse = Options.GetBool(OptionsKey.InvertMouse, false);

View File

@ -173,7 +173,7 @@ namespace ClassicalSharp {
public bool PureClassic { get { return ClassicMode && !ClassicHacks; } } public bool PureClassic { get { return ClassicMode && !ClassicHacks; } }
public bool UseCustomBlocks, UseCPE, UseServerTextures; public bool AllowCustomBlocks, UseCPE, AllowServerTextures;
public bool SmoothLighting; public bool SmoothLighting;

View File

@ -81,7 +81,7 @@ namespace ClassicalSharp.Map {
if (curCpeExt.ContainsKey("TextureURL")) if (curCpeExt.ContainsKey("TextureURL"))
url = (string)curCpeExt["TextureURL"].Value; url = (string)curCpeExt["TextureURL"].Value;
if (url.Length == 0) url = null; if (url.Length == 0) url = null;
if (game.UseServerTextures && url != null) if (game.AllowServerTextures && url != null)
game.Server.RetrieveTexturePack(url); game.Server.RetrieveTexturePack(url);
byte sidesBlock = (byte)curCpeExt["SideBlock"].Value; byte sidesBlock = (byte)curCpeExt["SideBlock"].Value;
@ -95,7 +95,7 @@ namespace ClassicalSharp.Map {
map.Env.SetWeather((Weather)weather); map.Env.SetWeather((Weather)weather);
} }
if (game.UseCustomBlocks && CheckKey("BlockDefinitions", 1, metadata)) { if (game.AllowCustomBlocks && CheckKey("BlockDefinitions", 1, metadata)) {
foreach (KeyValuePair<string, NbtTag> pair in curCpeExt) { foreach (KeyValuePair<string, NbtTag> pair in curCpeExt) {
if (pair.Value.TagId != NbtTagType.Compound) continue; if (pair.Value.TagId != NbtTagType.Compound) continue;
if (!Utils.CaselessStarts(pair.Key, "Block")) continue; if (!Utils.CaselessStarts(pair.Key, "Block")) continue;

View File

@ -71,7 +71,7 @@ namespace ClassicalSharp.Network {
} }
#if !ONLY_8BIT #if !ONLY_8BIT
else if (ext == "ExtendedBlocks") { else if (ext == "ExtendedBlocks") {
if (!game.UseCustomBlocks) return; if (!game.AllowCustomBlocks) return;
net.packetSizes[Opcode.SetBlock] += 1; net.packetSizes[Opcode.SetBlock] += 1;
net.packetSizes[Opcode.CpeHoldThis] += 1; net.packetSizes[Opcode.CpeHoldThis] += 1;
net.packetSizes[Opcode.CpeDefineBlock] += 1; net.packetSizes[Opcode.CpeDefineBlock] += 1;

View File

@ -13,7 +13,7 @@ namespace ClassicalSharp.Network.Protocols {
public override void Init() { Reset(); } public override void Init() { Reset(); }
public override void Reset() { public override void Reset() {
if (!game.UseCPE || !game.UseCustomBlocks) return; if (!game.UseCPE || !game.AllowCustomBlocks) return;
net.Set(Opcode.CpeDefineBlock, HandleDefineBlock, 80); net.Set(Opcode.CpeDefineBlock, HandleDefineBlock, 80);
net.Set(Opcode.CpeUndefineBlock, HandleRemoveBlockDefinition, 2); net.Set(Opcode.CpeUndefineBlock, HandleRemoveBlockDefinition, 2);
net.Set(Opcode.CpeDefineBlockExt, HandleDefineBlockExt, 85); net.Set(Opcode.CpeDefineBlockExt, HandleDefineBlockExt, 85);
@ -62,7 +62,7 @@ namespace ClassicalSharp.Network.Protocols {
} }
void HandleDefineBlockExt() { void HandleDefineBlockExt() {
if (!game.UseCustomBlocks) { if (!game.AllowCustomBlocks) {
net.SkipPacketData(Opcode.CpeDefineBlockExt); return; net.SkipPacketData(Opcode.CpeDefineBlockExt); return;
} }
BlockID block = HandleDefineBlockCommonStart(reader, net.cpeData.blockDefsExtVer >= 2); BlockID block = HandleDefineBlockCommonStart(reader, net.cpeData.blockDefsExtVer >= 2);

View File

@ -309,7 +309,7 @@ namespace ClassicalSharp.Network.Protocols {
void HandleSetMapEnvUrl() { void HandleSetMapEnvUrl() {
string url = reader.ReadString(); string url = reader.ReadString();
if (!game.UseServerTextures) return; if (!game.AllowServerTextures) return;
if (url == "") { if (url == "") {
if (game.World.TextureUrl != null) TexturePack.ExtractDefault(game); if (game.World.TextureUrl != null) TexturePack.ExtractDefault(game);
@ -459,9 +459,9 @@ namespace ClassicalSharp.Network.Protocols {
if (net.cpeData.ServerExtensionsCount != 0) return; if (net.cpeData.ServerExtensionsCount != 0) return;
string[] clientExts = CPESupport.ClientExtensions; string[] clientExts = CPESupport.ClientExtensions;
int count = clientExts.Length; int count = clientExts.Length;
if (!game.UseCustomBlocks) count -= 2; if (!game.AllowCustomBlocks) count -= 2;
#if !ONLY_8BIT #if !ONLY_8BIT
if (!game.UseCustomBlocks) count -= 1; if (!game.AllowCustomBlocks) count -= 1;
#endif #endif
WriteExtInfo(net.AppName, count); WriteExtInfo(net.AppName, count);
@ -473,10 +473,10 @@ namespace ClassicalSharp.Network.Protocols {
if (name == "EnvMapAppearance") ver = net.cpeData.envMapVer; if (name == "EnvMapAppearance") ver = net.cpeData.envMapVer;
if (name == "BlockDefinitionsExt") ver = net.cpeData.blockDefsExtVer; if (name == "BlockDefinitionsExt") ver = net.cpeData.blockDefsExtVer;
if (!game.UseCustomBlocks && name == "BlockDefinitionsExt") continue; if (!game.AllowCustomBlocks && name == "BlockDefinitionsExt") continue;
if (!game.UseCustomBlocks && name == "BlockDefinitions") continue; if (!game.AllowCustomBlocks && name == "BlockDefinitions") continue;
#if !ONLY_8BIT #if !ONLY_8BIT
if (!game.UseCustomBlocks && name == "ExtendedBlocks") continue; if (!game.AllowCustomBlocks && name == "ExtendedBlocks") continue;
#endif #endif
WriteExtEntry(name, ver); WriteExtEntry(name, ver);

View File

@ -131,7 +131,6 @@ String Block_DefaultName(BlockID block) {
if (end == -1) end = blockNames.length; if (end == -1) end = blockNames.length;
String buffer = String_InitAndClear(Block_NamePtr(block), STRING_SIZE); String buffer = String_InitAndClear(Block_NamePtr(block), STRING_SIZE);
Int32 i;
for (i = start; i < end; i++) { for (i = start; i < end; i++) {
String_Append(&buffer, blockNames.buffer[i]); String_Append(&buffer, blockNames.buffer[i]);
} }

View File

@ -20,6 +20,8 @@
#include "Block.h" #include "Block.h"
#include "Random.h" #include "Random.h"
#include "World.h" #include "World.h"
#include "Formats.h"
#include "ErrorHandler.h"
#define FILES_SCREEN_ITEMS 5 #define FILES_SCREEN_ITEMS 5
#define FILES_SCREEN_BUTTONS (FILES_SCREEN_ITEMS + 3) #define FILES_SCREEN_BUTTONS (FILES_SCREEN_ITEMS + 3)
@ -287,7 +289,7 @@ void ListScreen_ContextRecreated(void* obj) {
ListScreen_UpdateArrows(screen); ListScreen_UpdateArrows(screen);
} }
void ListScreen_Sort(Int32 left, Int32 right) { void ListScreen_QuickSort(Int32 left, Int32 right) {
StringsBuffer* buffer = &ListScreen_Instance.Entries; StringsBuffer* buffer = &ListScreen_Instance.Entries;
UInt32* keys = buffer->FlagsBuffer; UInt32 key; UInt32* keys = buffer->FlagsBuffer; UInt32 key;
while (left < right) { while (left < right) {
@ -305,6 +307,27 @@ void ListScreen_Sort(Int32 left, Int32 right) {
} }
} }
void ListScreen_AddFilename(void* obj, STRING_PURE String* path) {
/* folder1/folder2/entry.zip --> entry.zip */
Int32 lastDir = String_LastIndexOf(path, Platform_DirectorySeparator);
String filename = *path;
if (lastDir >= 0) {
filename = String_UNSAFE_SubstringAt(&filename, lastDir + 1);
}
StringsBuffer* entries = (StringsBuffer*)obj;
StringsBuffer_Add(entries, &filename);
}
void ListScreen_MakePath(ListScreen* screen, GuiElement* w, STRING_PURE String* path, const UInt8* dir, STRING_REF String* filename) {
Int32 idx = Menu_Index(screen->Widgets, Array_Elems(screen->Widgets), (Widget*)w);
*filename = StringsBuffer_UNSAFE_Get(&screen->Entries, screen->CurrentIndex + idx);
String_AppendConst(path, dir);
String_Append(path, Platform_DirectorySeparator);
String_AppendString(path, filename);
}
void ListScreen_Init(GuiElement* elem) { void ListScreen_Init(GuiElement* elem) {
ListScreen* screen = (ListScreen*)elem; ListScreen* screen = (ListScreen*)elem;
Platform_MakeFont(&screen->Font, &Game_FontName, 16, FONT_STYLE_BOLD); Platform_MakeFont(&screen->Font, &Game_FontName, 16, FONT_STYLE_BOLD);
@ -1189,18 +1212,12 @@ Screen* ClassicGenScreen_MakeInstance(void) {
void TexturePackScreen_EntryClick(GuiElement* screenElem, GuiElement* w) { void TexturePackScreen_EntryClick(GuiElement* screenElem, GuiElement* w) {
ListScreen* screen = (ListScreen*)screenElem; ListScreen* screen = (ListScreen*)screenElem;
UInt8 pathBuffer[String_BufferSize(FILENAME_SIZE)]; UInt8 pathBuffer[String_BufferSize(FILENAME_SIZE)];
String path = String_InitAndClearArray(pathBuffer); String path = String_InitAndClearArray(pathBuffer), filename;
ListScreen_MakePath(screen, w, &path, "texpacks", &filename);
Int32 curPage = screen->CurrentIndex;
Int32 idx = Menu_Index(screen->Widgets, Array_Elems(screen->Widgets), (Widget*)w);
String filename = StringsBuffer_UNSAFE_Get(&screen->Entries, curPage + idx);
String_AppendConst(&path, "texpacks");
String_Append(&path, Platform_DirectorySeparator);
String_Append(&path, &filename);
if (!Platform_FileExists(&path)) return; if (!Platform_FileExists(&path)) return;
game.DefaultTexturePack = filename; Int32 curPage = screen->CurrentIndex;
Game_SetDefaultTexturePack(&filename);
TexturePack_ExtractDefault(); TexturePack_ExtractDefault();
Elem_Recreate(screen); Elem_Recreate(screen);
ListScreen_SetCurrentIndex(screen, curPage); ListScreen_SetCurrentIndex(screen, curPage);
@ -1209,16 +1226,7 @@ void TexturePackScreen_EntryClick(GuiElement* screenElem, GuiElement* w) {
void TexturePackScreen_SelectEntry(STRING_PURE String* path, void* obj) { void TexturePackScreen_SelectEntry(STRING_PURE String* path, void* obj) {
String zip = String_FromConst(".zip"); String zip = String_FromConst(".zip");
if (!String_CaselessEnds(path, &zip)) return; if (!String_CaselessEnds(path, &zip)) return;
ListScreen_AddFilename(obj, path);
/* folder1/folder2/entry.zip --> entry.zip */
String filename = *path;
Int32 lastDir = String_LastIndexOf(&filename, Platform_DirectorySeparator);
if (lastDir >= 0) {
filename = String_UNSAFE_SubstringAt(&filename, lastDir + 1);
}
StringsBuffer* entries = (StringsBuffer*)obj;
StringsBuffer_Add(entries, &filename);
} }
Screen* TexturePackScreen_MakeInstance(void) { Screen* TexturePackScreen_MakeInstance(void) {
@ -1230,8 +1238,79 @@ Screen* TexturePackScreen_MakeInstance(void) {
String path = String_FromConst("texpacks"); String path = String_FromConst("texpacks");
Platform_EnumFiles(&path, &screen->Entries, TexturePackScreen_SelectEntry); Platform_EnumFiles(&path, &screen->Entries, TexturePackScreen_SelectEntry);
if (screen->Entries.Count > 0) { if (screen->Entries.Count > 0) {
ListScreen_Sort(0, screen->Entries.Count - 1); ListScreen_QuickSort(0, screen->Entries.Count - 1);
}
return (Screen*)screen;
}
/*########################################################################################################################*
*----------------------------------------------------LoadLevelScreen------------------------------------------------------*
*#########################################################################################################################*/
void LoadLevelScreen_SelectEntry(STRING_PURE String* path, void* obj) {
String cw = String_FromConst(".cw"); String lvl = String_FromConst(".lvl");
String fcm = String_FromConst(".fcm"); String dat = String_FromConst(".dat");
if (!(String_CaselessEnds(path, &cw) || String_CaselessEnds(path, &lvl)
|| String_CaselessEnds(path, &fcm) || String_CaselessEnds(path, &dat))) return;
ListScreen_AddFilename(obj, path);
}
void LoadLevelScreen_EntryClick(GuiElement* screenElem, GuiElement* w) {
ListScreen* screen = (ListScreen*)screenElem;
UInt8 pathBuffer[String_BufferSize(FILENAME_SIZE)];
String path = String_InitAndClearArray(pathBuffer), filename;
ListScreen_MakePath(screen, w, &path, "maps", &filename);
if (!Platform_FileExists(&path)) return;
void* file;
ReturnCode code = Platform_FileOpen(&file, &path, true);
ErrorHandler_CheckOrFail(code, "Failed to open map file");
Stream stream; Stream_FromFile(&stream, file, &path);
World_Reset();
Event_RaiseVoid(&WorldEvents_NewMap);
if (World_TextureUrl.length > 0) {
TexturePack_ExtractDefault();
String_Clear(&World_TextureUrl);
}
Block_Reset();
Inventory_SetDefaultMapping();
String cw = String_FromConst(".cw"); String lvl = String_FromConst(".lvl");
String fcm = String_FromConst(".fcm"); String dat = String_FromConst(".dat");
if (String_CaselessEnds(&path, &dat)) {
Dat_Load(&stream);
} else if (String_CaselessEnds(&path, &fcm)) {
Fcm_Load(&stream);
} else if (String_CaselessEnds(&path, &cw)) {
Cw_Load(&stream);
} else if (String_CaselessEnds(&path, &lvl)) {
Lvl_Load(&stream);
}
World_SetNewMap(World_Blocks, World_BlocksSize, World_Width, World_Height, World_Length);
Event_RaiseVoid(&WorldEvents_MapLoaded);
if (Game_AllowServerTextures && World_TextureUrl.length > 0) {
ServerConnection_RetrieveTexturePack(&World_TextureUrl);
}
LocalPlayer* p = &LocalPlayer_Instance;
LocationUpdate update; LocationUpdate_MakePosAndOri(&update, p->Spawn, p->SpawnRotY, p->SpawnHeadX, false);
p->Base.VTABLE->SetLocation(&p->Base, &update, false);
}
Screen* LoadLevelScreen_MakeInstance(void) {
ListScreen* screen = ListScreen_MakeInstance();
String title = String_FromConst("Select a level");
screen->TitleText = title;
screen->EntryClick = LoadLevelScreen_EntryClick;
String path = String_FromConst("maps");
Platform_EnumFiles(&path, &screen->Entries, LoadLevelScreen_SelectEntry);
if (screen->Entries.Count > 0) {
ListScreen_QuickSort(0, screen->Entries.Count - 1);
} }
return (Screen*)screen; return (Screen*)screen;
} }