diff --git a/protocoldb/protocoldb.go b/protocoldb/protocoldb.go new file mode 100644 index 0000000..c0f2e72 --- /dev/null +++ b/protocoldb/protocoldb.go @@ -0,0 +1,94 @@ +package protocoldb + +import ( + "fmt" + "io/fs" + "os" + "path/filepath" + + "gitea.mmo.to/ProtocolPacketForger/ppf/protocol" + "gitea.mmo.to/ProtocolPacketForger/ppf/protocolctl" +) + +type PPFDatabases struct { + Databases []ProtocolDatabaseProvider +} + +type ProtocolDatabaseProvider interface { + Open(path string) error + Close() error + Protocols() map[string]protocol.DOPMeta +} + +type ProtocolDatabaseManager interface { + Add(prot *protocol.ProtocolStructure, path string) + Update(prot *protocol.ProtocolStructure, path string) + // no remove. + UpdateDatabase() error // scans for new protocols +} + +type DatabaseEntry struct { + Path string + Protocol protocol.DOPMeta +} + +// FileDatabase implements ProtocolDatabaseProvider and ProtocolDatabaseManager +type FileDatabase struct { + Path string + protocols map[string]DatabaseEntry + closed bool +} + +type FileDatabaseFromGitRemote struct { + FileDatabase + RemoteGitRepository string +} + +func (fd *FileDatabase) Open(path string) error { + fileinfo, err := os.Stat(path) + if err == os.ErrNotExist { + return err + } + if !fileinfo.IsDir() { + return fmt.Errorf("Path %s is not a directory", path) + } + fd.Path = path + fd.closed = false + return nil +} + +func (fd *FileDatabase) Close() error { + for k := range fd.protocols { + delete(fd.protocols, k) + } + fd.closed = true + return nil +} + +func (fd *FileDatabase) Protocols() map[string]DatabaseEntry { + if !fd.closed { + fd.protocols = map[string]DatabaseEntry{} + // recurse recursively through the path + filepath.Walk(fd.Path, func(path string, info fs.FileInfo, err error) error { + if !info.IsDir() { + prot, err := protocolctl.LoadNew(path) + if err != nil { + // add to map + fd.protocols[prot.Metadata.Name] = DatabaseEntry{path, prot.Metadata} + } + } + return nil + }) + } + return fd.protocols +} + +func (fd *FileDatabase) Add(prot *protocol.ProtocolStructure, path string) { + +} +func (fd *FileDatabase) Update(prot *protocol.ProtocolStructure, path string) { + +} +func (fd *FileDatabase) UpdateDatabase() error { + +}