Initial commit

This commit is contained in:
Naruse
2024-11-07 23:25:15 +08:00
commit e8e5f3a1a7
246 changed files with 27356 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
import json
import traceback
from typing import Dict, Type, TypeVar, Optional, Any, List
from game_server.config.log import Error, Info
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import resource_registry
import game_server.resource.configdb # noqa: F401
T = TypeVar("T", bound=BaseResource)
def filter_data(cls: Type, data):
valid_fields = cls.__annotations__.keys()
return {field: data.get(field, None) for field in valid_fields}
class ResourceManager:
def __init__(self):
self.data: Dict[Type[T], Dict[Any, T]] = {}
def load_resources(self) -> None:
Info("[BOOT] [ResourceManager] Loading Resourcses...")
sorted_load_priority = dict(
sorted(resource_registry.items(), key=lambda item: item[1]["load_priority"])
).items()
for cls, metadata in sorted_load_priority:
path = metadata["path"]
try:
with open(path, "r", encoding="utf-8") as file:
raw_data = json.load(file)
except Exception:
Error(f"Error when loading resource {path}")
traceback.print_exc()
continue
self.data[cls] = {}
i = 0
for data in raw_data:
data = filter_data(cls, data)
item: T = cls(**data)
if not item.on_load():
continue
i += 1
index_value = item.get_index()
self.data[cls][index_value] = item
Info(f"[BOOT] [ResourceManager] Loaded {i} config(s) for {cls.__name__}")
def find_by_index(self, cls: Type[T], value: Any) -> Optional[T]:
return self.data.get(cls, {}).get(str(value))
def has_index(self, cls: Type[T], value: Any) -> bool:
return value in self.data.get(cls, {})
def values(self, cls: Type[T]) -> List[T]:
return list(self.data.get(cls, {}).values())
@staticmethod
def instance():
return resource_manager
resource_manager = ResourceManager()

View File

@@ -0,0 +1,15 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import TypeVar
T = TypeVar("T", bound="BaseResource")
@dataclass
class BaseResource(ABC):
def on_load(self: T) -> bool:
"""returns True by default if item loaded, otherwise will be skipped"""
return True
@abstractmethod
def get_index(self) -> str:
pass

View File

@@ -0,0 +1,15 @@
import importlib
import os
import sys
folder = "game_server/resource/configdb"
sys.path.append(os.path.dirname(folder))
for filename in os.listdir(folder):
if filename.endswith(".py") and filename != "__init__.py":
module_name = filename[:-3]
module_path = f"game_server.resource.configdb.{module_name}"
try:
importlib.import_module(module_path)
except Exception as e:
print(f"Error importing module '{module_path}': {e}")

View File

@@ -0,0 +1,12 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/ActChallengeData.json")
class ActChallengeData(BaseResource):
actId: int
difficulty: int
def get_index(self) -> str:
return str(self.actId)

View File

@@ -0,0 +1,11 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/ActivityTower.json")
class ActivityTowerData(BaseResource):
ActivityID: int
def get_index(self) -> str:
return str(self.ActivityID)

View File

@@ -0,0 +1,18 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/AvatarData.json")
class AvatarData(BaseResource):
avatarID: int
DefaultDressId: int
unlockStar: int
initialWeapon: int
skillList: list
def on_load(self) -> bool:
return self.avatarID != 316 and (self.avatarID < 9000 or self.avatarID > 20000)
def get_index(self) -> str:
return str(self.avatarID)

View File

@@ -0,0 +1,14 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/AvatarSubSkillData.json")
class AvatarSubSkillData(BaseResource):
skillId: int
unlockScoin: int
maxLv: int
avatarSubSkillId: int
def get_index(self) -> str:
return str(self.avatarSubSkillId)

View File

@@ -0,0 +1,11 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/AvatarTutorial.json")
class AvatarTutorialData(BaseResource):
ActivityID: int
def get_index(self) -> str:
return str(self.ActivityID)

View File

@@ -0,0 +1,11 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/Collection.json")
class CollectionData(BaseResource):
ID: int
def get_index(self) -> str:
return str(self.ID)

View File

@@ -0,0 +1,11 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/CustomHeadData.json")
class CustomHeadData(BaseResource):
headID: int
def get_index(self) -> str:
return str(self.headID)

View File

@@ -0,0 +1,12 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/DressData.json")
class DressData(BaseResource):
dressID: int
avatarIDList: list
def get_index(self) -> str:
return str(self.dressID)

View File

@@ -0,0 +1,26 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.configdb.elf_skill_data import ElfSkillData
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/Elf_AstraMate_Data.json")
class ElfAstraMateData(BaseResource):
ElfID: int
MaxLevel: int
MaxRarity: int
skill_lists: list[ElfSkillData]
def on_load(self) -> bool:
from game_server.resource import ResourceManager
self.skill_lists = [
skill
for skill in ResourceManager.instance().values(ElfSkillData)
if self.ElfID in skill.ElfIds
]
return True
def get_index(self) -> str:
return str(self.ElfID)

View File

@@ -0,0 +1,13 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource, LoadPriority
@dataclass
@GameResource("resources/ExcelOutputAsset/ElfSkillData.json", load_priority=LoadPriority.HIGH)
class ElfSkillData(BaseResource):
ElfSkillID: int
MaxLv: int
ElfIds: list
def get_index(self) -> str:
return str(self.ElfSkillID)

View File

@@ -0,0 +1,13 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/EntryThemeData.json")
class EntryThemeData(BaseResource):
SpaceShipConfigID: int
ThemeBGMConfigList: list
ThemeTagList: list
def get_index(self) -> str:
return str(self.SpaceShipConfigID)

View File

@@ -0,0 +1,11 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/EntryThemeItemData.json")
class EntryThemeItemData(BaseResource):
ThemeItemID: int
def get_index(self) -> str:
return str(self.ThemeItemID)

View File

@@ -0,0 +1,11 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/FrameData.json")
class Frame_Data(BaseResource):
id: int
def get_index(self) -> str:
return str(self.id)

View File

@@ -0,0 +1,14 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/MaterialData.json")
class MaterialData(BaseResource):
ID: int
rarity: int
maxRarity: int
quantityLimit: int
def get_index(self) -> str:
return str(self.ID)

View File

@@ -0,0 +1,13 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/MissionData.json")
class MissionData(BaseResource):
id: int
Priority: int
totalProgress: int
def get_index(self) -> str:
return str(self.id)

View File

@@ -0,0 +1,11 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/RecommendPanel.json")
class RecommendPanelData(BaseResource):
PanelID: int
def get_index(self) -> str:
return str(self.PanelID)

View File

@@ -0,0 +1,13 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/StageData_Main.json")
class StageDataMain(BaseResource):
levelId: int
maxProgress: int
challengeList: list
def get_index(self) -> str:
return str(self.levelId)

View File

@@ -0,0 +1,15 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/StepMissionCompensation.json")
class StepMissionCompensationData(BaseResource):
CompensationID: int
UnlockLevel: int
MainLineStepIDList: list
NewChallengeStepIDList: list
OldChallengeStepIDList: list
def get_index(self) -> str:
return str(self.CompensationID)

View File

@@ -0,0 +1,21 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/StigmataData.json")
class StigmataData(BaseResource):
ID: int
maxLv: int
rarity: int
maxRarity: int
evoID: int
quality: int
isSecurityProtect: bool
protect: bool
def on_load(self) -> bool:
return self.evoID == 0
def get_index(self) -> str:
return str(self.ID)

View File

@@ -0,0 +1,13 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/ThemeData_Avatar.json")
class ThemeDataAvatar(BaseResource):
AvatarData: int
BuffList: list[int]
avatarIDList: list[int]
def get_index(self) -> str:
return str(self.AvatarData)

View File

@@ -0,0 +1,17 @@
from dataclasses import dataclass
from game_server.resource.base_resource import BaseResource
from game_server.resource.decorators import GameResource
@dataclass
@GameResource("resources/ExcelOutputAsset/WeaponData.json")
class WeaponData(BaseResource):
ID: int
weaponMainID: int
maxLv: int
rarity: int
maxRarity: int
evoID: int
protect: bool
def get_index(self) -> str:
return str(self.ID)

View File

@@ -0,0 +1,17 @@
from typing import Type, Dict
resource_registry: Dict[Type, Dict[str, any]] = {}
class LoadPriority:
HIGH = 1
NORMAL = 2
LOW = 3
def GameResource(path: str, load_priority=LoadPriority.NORMAL):
def decorator(cls):
resource_registry[cls] = {"path": path, "load_priority": load_priority}
return cls
return decorator