public static List<string> GetLinkArray(ResourceContentFile cf)
 {
     if (cf.LinkCount == 0 || cf.Links == null) return new List<string>();
     else return new List<string>(cf.Links.Split('\n'));
 }
 public static byte[] GetTorrentData(ResourceContentFile cf)
 {
     return File.ReadAllBytes(GetTorrentPath(cf));
 }
        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;
        }
 public static string GetTorrentPath(ResourceContentFile cf)
 {
     return GetTorrentPath(cf.Resource.InternalName, cf.Md5);
 }
        static void UpdateLinks(ResourceContentFile content)
        {
            var valids = new List<string>();
            if (content.LinkCount > 0 || content.Links != null) valids = new List<string>(content.Links.Split('\n')); // get previous links

            if (content.FileName.EndsWith(".sdp")) return;

            if (!Debugger.IsAttached)
            {
                // should we use cached entries or run full check?
                if (content.Resource.LastLinkCheck != null)
                {
                    if (content.LinkCount > 0 &&
                        DateTime.UtcNow.Subtract(content.Resource.LastLinkCheck.Value).TotalMinutes < CheckPeriodForValidLinks) return;
                    if (content.LinkCount == 0 &&
                        DateTime.UtcNow.Subtract(content.Resource.LastLinkCheck.Value).TotalMinutes < CheckPeriodForMissingLinks) return;
                }
            }

            // get mirror list from jobjol
            foreach (var link in GetJobjolMirrorLinks(content.FileName, content.Resource.InternalName)) if (!valids.Contains(link)) valids.Add(link);

            // combine with hardcoded mirrors
            foreach (var url in Mirrors)
            {
                var replaced = url.Replace("%t", content.Resource.TypeID == ResourceType.Mod ? "mods" : "maps").Replace("%f", content.FileName);
                if (!valids.Contains(replaced)) valids.Add(replaced);
            }

            // check validity of all links at once

            Task.WaitAll(new List<string>(valids).Select(link => Task.Factory.StartNew(() => ValidateLink(link, content.Length, valids))).ToArray());

            valids = valids.Distinct().ToList();

            lock (content)
            {
                content.LinkCount = valids.Count;
                content.Resource.LastLinkCheck = DateTime.UtcNow;
                content.Links = string.Join("\n", valids.ToArray());
            }
        }
		private void detach_ResourceContentFiles(ResourceContentFile entity)
		{
			this.SendPropertyChanging();
			entity.Resource = null;
		}
 partial void DeleteResourceContentFile(ResourceContentFile instance);
 partial void UpdateResourceContentFile(ResourceContentFile instance);
 partial void InsertResourceContentFile(ResourceContentFile instance);