예제 #1
0
        public static DownloadFileResult DownloadFile(string internalName)
        {
            List <string> links;

            byte[]        torrent;
            List <string> dependencies;
            ResourceType  resourceType;
            string        torrentFileName;
            var           ok = ResourceLinkProvider.GetLinksAndTorrent(internalName,
                                                                       out links,
                                                                       out torrent,
                                                                       out dependencies,
                                                                       out resourceType,
                                                                       out torrentFileName);

            if (ok)
            {
                return(new DownloadFileResult()
                {
                    links = links,
                    torrent = torrent,
                    dependencies = dependencies,
                    resourceType = resourceType,
                    torrentFileName = torrentFileName,
                });
            }
            return(null);
        }
예제 #2
0
 public static bool DownloadFile(string internalName,
                                 out List <string> links,
                                 out byte[] torrent,
                                 out List <string> dependencies,
                                 out ResourceType resourceType,
                                 out string torrentFileName)
 {
     return(ResourceLinkProvider.GetLinksAndTorrent(internalName,
                                                    out links,
                                                    out torrent,
                                                    out dependencies,
                                                    out resourceType,
                                                    out torrentFileName));
 }
예제 #3
0
        public static ReturnValue RegisterResource(int apiVersion,
                                                   string springVersion,
                                                   string md5,
                                                   int length,
                                                   ResourceType resourceType,
                                                   string archiveName,
                                                   string internalName,
                                                   byte[] serializedData,
                                                   List <string> dependencies,
                                                   byte[] minimap,
                                                   byte[] metalMap,
                                                   byte[] heightMap,
                                                   byte[] torrentData)
        {
            if (md5 == null)
            {
                throw new ArgumentNullException("md5");
            }
            if (archiveName == null)
            {
                throw new ArgumentNullException("archiveName");
            }
            if (internalName == null)
            {
                throw new ArgumentNullException("internalName");
            }
            if (serializedData == null)
            {
                throw new ArgumentNullException("serializedData");
            }
            if (torrentData == null)
            {
                throw new ArgumentNullException("torrentData");
            }
            if (PlasmaServerApiVersion > apiVersion)
            {
                throw new Exception("Obsolete PlasmaServer Client");
            }
            if (dependencies == null)
            {
                dependencies = new List <string>();
            }

            var db = new ZkDataContext();


            var contentFile = db.ResourceContentFiles.FirstOrDefault(x => x.Md5 == md5);

            if (contentFile != null)
            {
                // content file already stored
                if (contentFile.Resource.InternalName != internalName)
                {
                    return(ReturnValue.Md5AlreadyExistsWithDifferentName);
                }

                // new spring version we add its hash
                StoreMetadata(md5, contentFile.Resource, serializedData, torrentData, minimap, metalMap, heightMap);
                db.SaveChanges();
                return(ReturnValue.Ok);
            }

            var resource = db.Resources.Where(x => x.InternalName == internalName).SingleOrDefault();

            if (resource == null)
            {
                resource = new Resource {
                    InternalName = internalName, TypeID = resourceType
                };
                db.Resources.Add(resource);
                StoreMetadata(md5, resource, serializedData, torrentData, minimap, metalMap, heightMap);
            }

            if (!resource.ResourceDependencies.Select(x => x.NeedsInternalName).Except(dependencies).Any())
            {
                // new dependencies are superset
                foreach (var depend in dependencies)
                {
                    // add missing dependencies
                    var s = depend;
                    if (!resource.ResourceDependencies.Any(x => x.NeedsInternalName == s))
                    {
                        resource.ResourceDependencies.Add(new ResourceDependency {
                            NeedsInternalName = depend
                        });
                    }
                }
            }

            if (resource.ResourceContentFiles.Any(x => x.Length == length && x.Md5 != md5))
            {
                return(ReturnValue.Md5AlreadyExistsWithDifferentName);
                // add proper message - file exists with different md5 and same size - cant register cant detect mirrors
            }

            var newContentFile = new ResourceContentFile {
                FileName = archiveName, Length = length, Md5 = md5, Resource = resource
            };

            resource.ResourceContentFiles.Add(newContentFile);
            ResourceLinkProvider.UpdateLinks(newContentFile);
            File.WriteAllBytes(GetTorrentPath(internalName, md5), torrentData); // add new torrent file

            db.SaveChanges();



            return(ReturnValue.Ok);
        }