comparison scn2k/scn2k_impl.cc @ 53:ddbcbd000206

* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons * ParseMoji moved to TextStream * Some cleaning (0 -> NULL when needed, removal of useless returns, ...)
author thib
date Sun, 19 Apr 2009 11:44:05 +0000
parents 15a18fbe6f21
children 6d9146f56ccf
comparison
equal deleted inserted replaced
52:15a18fbe6f21 53:ddbcbd000206
43 void kconv(const unsigned char* src, unsigned char* dest); 43 void kconv(const unsigned char* src, unsigned char* dest);
44 void kconv_rev(const unsigned char* src, unsigned char* dest); 44 void kconv_rev(const unsigned char* src, unsigned char* dest);
45 string kconv(const string& s); 45 string kconv(const string& s);
46 string kconv_rev(const string& s); 46 string kconv_rev(const string& s);
47 47
48 Scn2k::Scn2k(Event::Container& _event, PicContainer& _parent, class MuSys& mu, AyuSysConfig& _config) : 48 Scn2k::Scn2k(Event::Container& _event, PicContainer& _parent) :
49 Event::Time(_event), 49 Event::Time(_event),
50 event(_event), 50 event(_event),
51 parent(_parent), 51 parent(_parent),
52 config(_config), 52 text_exec(_event, _parent),
53 text_exec(_event, _parent, config), 53 grp_exec(_event, _parent, flag, flag.cgm_data)
54 grp_exec(_event, _parent, flag, flag.cgm_data, mu, config)
55 { 54 {
55 config = AyuSysConfig::GetInstance();
56
56 system_version = 0; 57 system_version = 0;
57 skip_mode = SKIP_NO; 58 skip_mode = SKIP_NO;
58 59
59 script_start = NULL; 60 script_start = NULL;
60 script = NULL; 61 script = NULL;
81 mouse_pressed = 0; 82 mouse_pressed = 0;
82 ShowCursor(); 83 ShowCursor();
83 84
84 LoadSys(); 85 LoadSys();
85 text_exec.InitWindow(); 86 text_exec.InitWindow();
86 grp_exec.InitSel(config); 87 grp_exec.InitSel();
87 } 88 }
88 89
89 Scn2k::~Scn2k() { 90 Scn2k::~Scn2k() {
90 if (script_start) delete[] script_start; 91 if (script_start) delete[] script_start;
91 HideCursor(); 92 HideCursor();
97 const char* data; 98 const char* data;
98 char* ret_data; 99 char* ret_data;
99 int offset = 0; 100 int offset = 0;
100 int scenario_magic; 101 int scenario_magic;
101 102
103 FileSearcher* file_searcher = FileSearcher::GetInstance();
104
102 sprintf(fname, "SEEN%04d.TXT", new_scn_number); 105 sprintf(fname, "SEEN%04d.TXT", new_scn_number);
103 ARCINFO* info = file_searcher.Find(FILESEARCH::SCN, fname, ""); 106 ARCINFO* info = file_searcher->Find(FileSearcher::SCN, fname, "");
104 if (info == NULL) goto err; 107 if (info == NULL) goto err;
105 data = info->Read(); 108 data = info->Read();
106 109
107 /* version 確認 */ 110 /* version 確認 */
108 scenario_magic = read_little_endian_int(data + 4); 111 scenario_magic = read_little_endian_int(data + 4);
388 391
389 void Scn2k::ShowCursor(void) { 392 void Scn2k::ShowCursor(void) {
390 HideCursor(); 393 HideCursor();
391 char key[1024]; 394 char key[1024];
392 sprintf(key, "#MOUSE_CURSOR.%03d.NAME",mouse_type); 395 sprintf(key, "#MOUSE_CURSOR.%03d.NAME",mouse_type);
393 const char* name = config.GetParaStr(key); 396 const char* name = config->GetParaStr(key);
394 if (name == NULL || name[0] == 0) mouse_surface = DEFAULT_MOUSECURSOR; 397 if (name == NULL || name[0] == 0) mouse_surface = DEFAULT_MOUSECURSOR;
395 else { 398 else {
396 mouse_surface = parent.Root().NewSurface(name, COLOR_MASK); 399 mouse_surface = parent.Root().NewSurface(name, COLOR_MASK);
397 } 400 }
398 if (mouse_surface == NULL) mouse_surface = DEFAULT_MOUSECURSOR; 401 if (mouse_surface == NULL) mouse_surface = DEFAULT_MOUSECURSOR;
483 ShowCursor(); 486 ShowCursor();
484 return; 487 return;
485 } 488 }
486 if (cmd.cmd_type == CMD_MENUREQ) { 489 if (cmd.cmd_type == CMD_MENUREQ) {
487 int scn=0, pt=0; 490 int scn=0, pt=0;
488 config.GetParam("#CANCELCALL", 2, &scn, &pt); 491 config->GetParam("#CANCELCALL", 2, &scn, &pt);
489 if (scn) { 492 if (scn) {
490 // 右クリックされたら global call を行う 493 // 右クリックされたら global call を行う
491 cmd.cmd_type = CMD_OTHER; 494 cmd.cmd_type = CMD_OTHER;
492 cmd.cmd1 = 0; 495 cmd.cmd1 = 0;
493 cmd.cmd2 = 1; 496 cmd.cmd2 = 1;
548 if (cmd.args.size() >= 2) { // subroutine number が付く 551 if (cmd.args.size() >= 2) { // subroutine number が付く
549 // 引数が付くのもあるらしい 552 // 引数が付くのもあるらしい
550 new_pt = cmd.args[1].value; 553 new_pt = cmd.args[1].value;
551 } 554 }
552 if (new_scn == SCN_INFO_MENU) { // menu call 555 if (new_scn == SCN_INFO_MENU) { // menu call
553 config.GetParam("#CANCELCALL", 2, &new_scn, &new_pt); 556 config->GetParam("#CANCELCALL", 2, &new_scn, &new_pt);
554 stack.push_back(StackItem(SCN_INFO, SCN_INFO_MENU)); // menu call を示す特殊な記号 557 stack.push_back(StackItem(SCN_INFO, SCN_INFO_MENU)); // menu call を示す特殊な記号
555 } else { 558 } else {
556 int i; 559 int i;
557 VarInfo var; 560 VarInfo var;
558 // ローカル変数を伴う subroutine call 561 // ローカル変数を伴う subroutine call
699 } 702 }
700 script = script_start + s.scn_pt; 703 script = script_start + s.scn_pt;
701 cmd.clear(); 704 cmd.clear();
702 } 705 }
703 } else if (cmd.cmd1 == 2 && cmd.cmd2 == 1 && cmd.cmd3 == 12) { // DLL Call 706 } else if (cmd.cmd1 == 2 && cmd.cmd2 == 1 && cmd.cmd3 == 12) { // DLL Call
704 const char* regname = config.GetParaStr("#REGNAME"); 707 const char* regname = config->GetParaStr("#REGNAME");
705 const char key_lb[] = "KEY\\LittleBusters";//FIXME: too specific to be here? 708 const char key_lb[] = "KEY\\LittleBusters";//FIXME: too specific to be here?
706 if (strcmp(regname, key_lb) == 0) { 709 if (strcmp(regname, key_lb) == 0) {
707 DllCall_LB(cmd, flag); 710 DllCall_LB(cmd, flag);
708 cmd.clear(); 711 cmd.clear();
709 } 712 }
717 } else if (cmd.cmd1 == 1 && cmd.cmd2 == 0x04) { 720 } else if (cmd.cmd1 == 1 && cmd.cmd2 == 0x04) {
718 if (cmd.cmd3 == 0 && cmd.cmd4 == 0) { // タイトル名設定 721 if (cmd.cmd3 == 0 && cmd.cmd4 == 0) { // タイトル名設定
719 const char* name = cmd.Str(cmd.args[0]); 722 const char* name = cmd.Str(cmd.args[0]);
720 if (name == NULL) name = ""; 723 if (name == NULL) name = "";
721 window_title = name; 724 window_title = name;
722 const char* config_name = config.GetParaStr("#CAPTION"); 725 const char* config_name = config->GetParaStr("#CAPTION");
723 if (config_name == NULL) config_name = ""; 726 if (config_name == NULL) config_name = "";
724 string setname = kconv(string(config_name) + " " + window_title); 727 string setname = kconv(string(config_name) + " " + window_title);
725 parent.Root().SetWindowCaption(setname.c_str()); 728 parent.Root().SetWindowCaption(setname.c_str());
726 cmd.clear(); 729 cmd.clear();
727 } else if (cmd.cmd3 == 0x82 && cmd.cmd4 == 0) { 730 } else if (cmd.cmd3 == 0x82 && cmd.cmd4 == 0) {
759 } else if (cmd.cmd3 == 0x4b4 || cmd.cmd3 == 0x4b5) { // 選択肢巻き戻し 762 } else if (cmd.cmd3 == 0x4b4 || cmd.cmd3 == 0x4b5) { // 選択肢巻き戻し
760 LoadRollback(cmd); 763 LoadRollback(cmd);
761 } else if (cmd.cmd3 == 0x58d) { 764 } else if (cmd.cmd3 == 0x58d) {
762 // 前にロード|セーブされた番号を返す。 765 // 前にロード|セーブされた番号を返す。
763 int lastsave; 766 int lastsave;
764 config.GetParam("#LASTSAVE", 1, &lastsave); 767 config->GetParam("#LASTSAVE", 1, &lastsave);
765 flag.SetSys(lastsave-1); 768 flag.SetSys(lastsave-1);
766 } else if (cmd.cmd3 == 0x585) { 769 } else if (cmd.cmd3 == 0x585) {
767 // 第一引数の記録された日付、タイトルなどが返される 770 // 第一引数の記録された日付、タイトルなどが返される
768 // データがないなら sys に 0が、あるなら 1 が返る 771 // データがないなら sys に 0が、あるなら 1 が返る
769 int y,m,d,wd,h,min,s,ms; 772 int y,m,d,wd,h,min,s,ms;
790 Save(cmd); 793 Save(cmd);
791 } else if (cmd.cmd3 == 0xc25) { // load 794 } else if (cmd.cmd3 == 0xc25) { // load
792 Load(cmd); 795 Load(cmd);
793 } else if (cmd.cmd3 == 0x4b1 || cmd.cmd3 == 0x4b3) { // menu へ戻る (4b3: バッドエンド) 796 } else if (cmd.cmd3 == 0x4b1 || cmd.cmd3 == 0x4b3) { // menu へ戻る (4b3: バッドエンド)
794 int scn_start; 797 int scn_start;
795 if (config.GetParam("#SEEN_MENU", 1, &scn_start) == 0) { 798 if (config->GetParam("#SEEN_MENU", 1, &scn_start) == 0) {
796 ChangeScript(scn_start, 0); 799 ChangeScript(scn_start, 0);
797 save_scn = 0; 800 save_scn = 0;
798 save_point = 0; 801 save_point = 0;
799 window_title = ""; 802 window_title = "";
800 const char* window_title_config = config.GetParaStr("#CAPTION"); 803 const char* window_title_config = config->GetParaStr("#CAPTION");
801 if (window_title_config) window_title = window_title_config; 804 if (window_title_config) window_title = window_title_config;
802 parent.Root().SetWindowCaption(kconv(window_title).c_str()); 805 parent.Root().SetWindowCaption(kconv(window_title).c_str());
803 stack.clear(); 806 stack.clear();
804 cmd_stack.clear(); 807 cmd_stack.clear();
805 cmd_stack_str = cmd_stack_str_orig; 808 cmd_stack_str = cmd_stack_str_orig;
856 if ( (sstatus.st_mode & S_IFMT) == S_IFREG) { 859 if ( (sstatus.st_mode & S_IFMT) == S_IFREG) {
857 return dir; 860 return dir;
858 } 861 }
859 } 862 }
860 // ファイル名を作る 863 // ファイル名を作る
861 const char* regname = config.GetParaStr("#REGNAME"); 864 const char* regname = config->GetParaStr("#REGNAME");
862 865
863 char* fname = new char[strlen(regname)+1]; 866 char* fname = new char[strlen(regname)+1];
864 /* レジストリ名をファイル名として有効なものにする */ 867 /* レジストリ名をファイル名として有効なものにする */
865 int i; for (i=0; regname[i]!=0; i++) { 868 int i; for (i=0; regname[i]!=0; i++) {
866 char c = regname[i]; 869 char c = regname[i];
890 void Scn2k::SaveSys(void) { 893 void Scn2k::SaveSys(void) {
891 char buf[1024]; 894 char buf[1024];
892 string save; 895 string save;
893 string path = MakeSaveFile(); 896 string path = MakeSaveFile();
894 897
895 sprintf(buf, "KEY=%s\n", config.GetParaStr("#REGNAME")); 898 sprintf(buf, "KEY=%s\n", config->GetParaStr("#REGNAME"));
896 save += buf; 899 save += buf;
897 string save_config; 900 string save_config;
898 config.DiffOriginal(save_config); 901 config->DiffOriginal(save_config);
899 save += "CONFIG="; 902 save += "CONFIG=";
900 save += save_config; 903 save += save_config;
901 save += "\n"; 904 save += "\n";
902 string save_flag; flag.SaveSys(save_flag); 905 string save_flag; flag.SaveSys(save_flag);
903 save += save_flag; 906 save += save_flag;
944 char* savedata = new char[sz+1]; 947 char* savedata = new char[sz+1];
945 fread(savedata, sz, 1, f); 948 fread(savedata, sz, 1, f);
946 savedata[sz] = 0; 949 savedata[sz] = 0;
947 fclose(f); 950 fclose(f);
948 951
949 sprintf(buf, "KEY=%s\n", config.GetParaStr("#REGNAME")); 952 sprintf(buf, "KEY=%s\n", config->GetParaStr("#REGNAME"));
950 if (strncmp(savedata, buf, strlen(buf)) != 0) { 953 if (strncmp(savedata, buf, strlen(buf)) != 0) {
951 fprintf(stderr,"Invalid header in save file %s: it must be started with \"%s\"\n", path.c_str(), buf); 954 fprintf(stderr,"Invalid header in save file %s: it must be started with \"%s\"\n", path.c_str(), buf);
952 } else { 955 } else {
953 char* config_str = strstr(savedata, "\nCONFIG="); 956 char* config_str = strstr(savedata, "\nCONFIG=");
954 if (config_str) { 957 if (config_str) {
957 if (strend) { 960 if (strend) {
958 int l = strend - config_str; 961 int l = strend - config_str;
959 char* config_copy = new char[l+1]; 962 char* config_copy = new char[l+1];
960 strncpy(config_copy, config_str, l); 963 strncpy(config_copy, config_str, l);
961 config_copy[l] = 0; 964 config_copy[l] = 0;
962 config.PatchOriginal(config_copy); 965 config->PatchOriginal(config_copy);
963 delete[] config_copy; 966 delete[] config_copy;
964 } 967 }
965 } 968 }
966 flag.LoadSys(savedata); 969 flag.LoadSys(savedata);
967 grp_exec.LoadSys(savedata); 970 grp_exec.LoadSys(savedata);
995 } 998 }
996 delete[] savedata; 999 delete[] savedata;
997 } 1000 }
998 1001
999 /* 初期化 */ 1002 /* 初期化 */
1000 int scn_start; config.GetParam("#SEEN_START", 1, &scn_start); 1003 int scn_start; config->GetParam("#SEEN_START", 1, &scn_start);
1001 ChangeScript(scn_start, 0); 1004 ChangeScript(scn_start, 0);
1002 save_scn = 0; 1005 save_scn = 0;
1003 save_point = 0; 1006 save_point = 0;
1004 window_title = ""; 1007 window_title = "";
1005 const char* window_title_config = config.GetParaStr("#CAPTION"); 1008 const char* window_title_config = config->GetParaStr("#CAPTION");
1006 if (window_title_config) window_title = window_title_config; 1009 if (window_title_config) window_title = window_title_config;
1007 parent.Root().SetWindowCaption(kconv(window_title).c_str()); 1010 parent.Root().SetWindowCaption(kconv(window_title).c_str());
1008 stack.clear(); 1011 stack.clear();
1009 cmd_stack.clear(); 1012 cmd_stack.clear();
1010 cmd_stack_str = cmd_stack_str_orig; 1013 cmd_stack_str = cmd_stack_str_orig;
1031 msec = 0; 1034 msec = 0;
1032 /* タイトルの取得 */ 1035 /* タイトルの取得 */
1033 FILE* savefile = fopen(path.c_str(), "rb"); 1036 FILE* savefile = fopen(path.c_str(), "rb");
1034 if (savefile == NULL) return false; 1037 if (savefile == NULL) return false;
1035 char regname[1024]; 1038 char regname[1024];
1036 sprintf(regname, "KEY=%s\n", config.GetParaStr("#REGNAME")); 1039 sprintf(regname, "KEY=%s\n", config->GetParaStr("#REGNAME"));
1037 fgets(buf,1000,savefile); 1040 fgets(buf,1000,savefile);
1038 if (strncmp(regname, buf, strlen(regname)) != 0) { 1041 if (strncmp(regname, buf, strlen(regname)) != 0) {
1039 fprintf(stderr,"invalid save file %s (registory name is not %s)\n",path.c_str(),regname); 1042 fprintf(stderr,"invalid save file %s (registory name is not %s)\n",path.c_str(),regname);
1040 fclose(savefile); 1043 fclose(savefile);
1041 return false; 1044 return false;
1123 sprintf(buf, ".%d",file_number); 1126 sprintf(buf, ".%d",file_number);
1124 path += buf; 1127 path += buf;
1125 1128
1126 /* セーブファイル確認 */ 1129 /* セーブファイル確認 */
1127 1130
1128 sprintf(buf, "KEY=%s\n", config.GetParaStr("#REGNAME")); save += buf; 1131 sprintf(buf, "KEY=%s\n", config->GetParaStr("#REGNAME")); save += buf;
1129 string save_sys; SaveImpl(save_sys); 1132 string save_sys; SaveImpl(save_sys);
1130 string save_flag; flag.Save(save_flag); 1133 string save_flag; flag.Save(save_flag);
1131 string save_text; text_exec.Save(save_text, false); 1134 string save_text; text_exec.Save(save_text, false);
1132 string save_grp; grp_exec.Save(save_grp); 1135 string save_grp; grp_exec.Save(save_grp);
1133 save += save_sys; 1136 save += save_sys;
1146 fprintf(stderr,"Cannot open save file %s\n",path.c_str()); 1149 fprintf(stderr,"Cannot open save file %s\n",path.c_str());
1147 return; 1150 return;
1148 } 1151 }
1149 fwrite(save.c_str(), save.length(), 1, f); 1152 fwrite(save.c_str(), save.length(), 1, f);
1150 fclose(f); 1153 fclose(f);
1151 config.SetParam("#LASTSAVE", 1, file_number); 1154 config->SetParam("#LASTSAVE", 1, file_number);
1152 cmd.clear(); 1155 cmd.clear();
1153 } 1156 }
1154 1157
1155 void Scn2k::Load(Cmd& cmd) { 1158 void Scn2k::Load(Cmd& cmd) {
1156 if (cmd.cmd_type == CMD_LOADREQ) { 1159 if (cmd.cmd_type == CMD_LOADREQ) {
1185 char* savedata = new char[sz+1]; 1188 char* savedata = new char[sz+1];
1186 fread(savedata, sz, 1, f); 1189 fread(savedata, sz, 1, f);
1187 savedata[sz] = 0; 1190 savedata[sz] = 0;
1188 fclose(f); 1191 fclose(f);
1189 1192
1190 sprintf(buf, "KEY=%s\n", config.GetParaStr("#REGNAME")); 1193 sprintf(buf, "KEY=%s\n", config->GetParaStr("#REGNAME"));
1191 if (strncmp(savedata, buf, strlen(buf)) != 0) { 1194 if (strncmp(savedata, buf, strlen(buf)) != 0) {
1192 fprintf(stderr,"Invalid header in save file %s: it must be started with \"%s\"\n", path.c_str(), buf); 1195 fprintf(stderr,"Invalid header in save file %s: it must be started with \"%s\"\n", path.c_str(), buf);
1193 delete[] savedata; 1196 delete[] savedata;
1194 return; 1197 return;
1195 } 1198 }
1273 } else if (strncmp(save, "Title=", 6) == 0) { 1276 } else if (strncmp(save, "Title=", 6) == 0) {
1274 save += 6; 1277 save += 6;
1275 char* s = strchr(save, '\n'); 1278 char* s = strchr(save, '\n');
1276 if (s == NULL) window_title = save; 1279 if (s == NULL) window_title = save;
1277 else window_title.assign(save, s-save); 1280 else window_title.assign(save, s-save);
1278 const char* config_name = config.GetParaStr("#CAPTION"); 1281 const char* config_name = config->GetParaStr("#CAPTION");
1279 if (config_name == NULL) config_name = ""; 1282 if (config_name == NULL) config_name = "";
1280 string setname = kconv(string(config_name)+" "+window_title); 1283 string setname = kconv(string(config_name)+" "+window_title);
1281 parent.Root().SetWindowCaption(setname.c_str()); 1284 parent.Root().SetWindowCaption(setname.c_str());
1282 } else if (strncmp(save, "MouseType=", 10) == 0) { 1285 } else if (strncmp(save, "MouseType=", 10) == 0) {
1283 sscanf(save, "MouseType=%d", &mouse_type); 1286 sscanf(save, "MouseType=%d", &mouse_type);
1896 switch(type) { 1899 switch(type) {
1897 case MENU_LOAD: pimpl = new LoadMenu(*this); break; 1900 case MENU_LOAD: pimpl = new LoadMenu(*this); break;
1898 case MENU_SAVE: pimpl = new LoadMenu(*this); break; 1901 case MENU_SAVE: pimpl = new LoadMenu(*this); break;
1899 case MENU_BACKLOG: pimpl = new BacklogMenu(*this, scn_impl, text_exec); break; 1902 case MENU_BACKLOG: pimpl = new BacklogMenu(*this, scn_impl, text_exec); break;
1900 } 1903 }
1901 return; 1904 }
1902 } 1905
1903 Scn2kMenu::~Scn2kMenu() { 1906 Scn2kMenu::~Scn2kMenu() {
1904 if (pimpl) delete pimpl; 1907 if (pimpl) delete pimpl;
1905 pimpl = NULL; 1908 pimpl = NULL;
1906 } 1909 }
1907 1910