//go:build windows package cafegame import ( "encoding/binary" "os" "path/filepath" "strconv" "testing" ) // newYGXGameDir 构造一个云更新游戏目录(gamepatch/gconfig.ini + 数字 ico) func newYGXGameDir(t *testing.T, id int) string { dir := filepath.Join(t.TempDir(), "测试游戏") if err := os.MkdirAll(filepath.Join(dir, "gamepatch"), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "gamepatch", "gconfig.ini"), nil, 0644); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, strconv.Itoa(id)+".ico"), nil, 0644); err != nil { t.Fatal(err) } return dir } func TestIsGameDirByYGX(t *testing.T) { dir := newYGXGameDir(t, 40001) ok, info, err := IsGameDirByYGX(dir) if err != nil { t.Fatalf("IsGameDirByYGX: %v", err) } if !ok { t.Fatal("云更新目录应识别成功") } if info.AppId != 40001 { t.Errorf("AppId = %d, want 40001", info.AppId) } if info.AppSource != SourceYGX { t.Errorf("AppSource = %d, want SourceYGX(%d)", info.AppSource, SourceYGX) } if info.AppName != "测试游戏" { t.Errorf("AppName = %q", info.AppName) } } func TestIsGameDirByYGXNegative(t *testing.T) { // 空目录(无 gamepatch) if ok, _, _ := IsGameDirByYGX(t.TempDir()); ok { t.Error("空目录不应识别为云更新") } // 有 gamepatch/gconfig.ini 但 ico 编号过小 dir := filepath.Join(t.TempDir(), "small") _ = os.MkdirAll(filepath.Join(dir, "gamepatch"), 0755) _ = os.WriteFile(filepath.Join(dir, "gamepatch", "gconfig.ini"), nil, 0644) _ = os.WriteFile(filepath.Join(dir, "12345.ico"), nil, 0644) if ok, _, _ := IsGameDirByYGX(dir); ok { t.Error("ico 编号小于 40000 不应识别为云更新") } } func TestIsGameDirByStory(t *testing.T) { dir := newYGXGameDir(t, 10001) ok, info, err := IsGameDirByStory(dir) if err != nil { t.Fatalf("IsGameDirByStory: %v", err) } if !ok { t.Fatal("蘑菇目录应识别成功") } if info.AppSource != SourceStory { t.Errorf("AppSource = %d, want SourceStory(%d)", info.AppSource, SourceStory) } } func TestGetGuidByYLY(t *testing.T) { // 构造易乐游 $ResMetadata$/ResAppData.dat:24 字节头 + GUID 结构 dir := t.TempDir() data := make([]byte, 40) binary.LittleEndian.PutUint32(data[24:28], 0x12345678) binary.LittleEndian.PutUint16(data[28:30], 0xABCD) binary.LittleEndian.PutUint16(data[30:32], 0xEF01) copy(data[32:36], []byte{1, 2, 3, 4}) copy(data[36:40], []byte{5, 6, 7, 8}) if err := os.MkdirAll(filepath.Join(dir, "$ResMetadata$"), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "$ResMetadata$", "ResAppData.dat"), data, 0644); err != nil { t.Fatal(err) } guid, err := GetGuidByYLY(dir) if err != nil { t.Fatalf("GetGuidByYLY: %v", err) } want := "{12345678-ABCD-EF01-0102-030405060708}" if guid != want { t.Errorf("GUID = %q, want %q", guid, want) } }