private void PopulateMissions(MissionFilter missionFilter)
        {
            ClearMissions();

            List <MissionData> missions;

            // Show local content even if it is no longer provided by the database
            if (missionFilter == MissionFilter.Library)
            {
                missions = new List <MissionData>(CacheDetails.missions);

                // Create database lookup table
                Dictionary <uint, MissionData> lookupTable = new Dictionary <uint, MissionData>();
                foreach (MissionData mission in m_Missions)
                {
                    lookupTable[mission.missionID] = mission;
                }

                // Replace local mission data with the database data
                for (int i = 0; i < missions.Count; ++i)
                {
                    MissionData dbMissionData;
                    if (!lookupTable.TryGetValue(missions[i].missionID, out dbMissionData))
                    {
                        continue;
                    }

                    missions[i] = dbMissionData;
                }
            }
            else
            {
                // Use the database data
                missions = new List <MissionData>(m_Missions);
            }

            foreach (MissionData mission in missions)
            {
                // Determine is mission is a standard mission
                bool hasDLL            = mission.fileNames.FirstOrDefault((filename) => filename.IndexOf(".dll", System.StringComparison.OrdinalIgnoreCase) >= 0) != null;
                bool hasOPM            = mission.fileNames.FirstOrDefault((filename) => filename.IndexOf(".opm", System.StringComparison.OrdinalIgnoreCase) >= 0) != null;
                bool isStandardMission = !hasDLL && hasOPM;

                //bool isInstalled = File.Exists(CachePath.GetMissionInstalledMetaFilePath(mission.missionID));
                bool isDownloaded  = File.Exists(CachePath.GetMissionDetailsFilePath(mission.missionID));
                bool isYourContent = AppController.localUser.isLoggedIn && AppController.localUser.userID == mission.authorID;

                if (missionFilter == MissionFilter.Standard && !isStandardMission)
                {
                    continue;
                }
                if (missionFilter == MissionFilter.Custom && isStandardMission)
                {
                    continue;
                }
                if (missionFilter == MissionFilter.Library && !isDownloaded)
                {
                    continue;
                }
                if (missionFilter == MissionFilter.YourContent && !isYourContent)
                {
                    continue;
                }

                // Create mission item
                GameObject goItem = Instantiate(m_MissionListItemPrefab);
                goItem.GetComponent <MissionListItem>().onUpdatedMissionCB += Refresh;
                goItem.GetComponent <MissionListItem>().Initialize(mission);
                goItem.GetComponent <MissionListItem>().hasAuthorPermissions = AppController.localUser.isLoggedIn && AppController.localUser.userID == mission.authorID;
                goItem.GetComponent <MissionListItem>().hasAdminPermissions  = AppController.localUser.isLoggedIn && AppController.localUser.isAdmin;

                goItem.transform.SetParent(m_MissionContainer);
            }

            if (missionFilter == MissionFilter.YourContent && AppController.localUser.isLoggedIn)
            {
                // Create blank mission item
                GameObject goItem = Instantiate(m_MissionListItemPrefab);
                goItem.GetComponent <MissionListItem>().onUpdatedMissionCB += Refresh;
                goItem.GetComponent <MissionListItem>().canEdit             = true;
                goItem.GetComponent <MissionListItem>().hasAdminPermissions = AppController.localUser.isLoggedIn && AppController.localUser.isAdmin;

                goItem.transform.SetParent(m_MissionContainer);
            }

            OnValueChanged_SearchFilter();
        }
Esempio n. 2
0
        private IEnumerator RequestDownloadMission()
        {
            ProgressDialog progressDialog = ProgressDialog.Create("Downloading Mission");

            foreach (string fileName in missionData.fileNames)
            {
                // Perform request
                string url      = WebConfig.webHost + "download/" + missionData.missionID + "/" + fileName;
                string destPath = Path.Combine(CachePath.GetMissionDirectory(missionData.missionID), fileName);

                using (UnityWebRequest request = UnityWebRequest.Get(url))
                {
                    progressDialog.SetTitle("Downloading " + fileName);
                    progressDialog.SetWebRequest(request);

                    yield return(request.SendWebRequest());

                    if (!DidDownloadSucceed(request, true))
                    {
                        progressDialog.Close();
                        OnClick_Delete();
                        yield break;
                    }

                    WriteFile(destPath, request.downloadHandler.data);
                }
            }

            // Get SDK path
            if (!string.IsNullOrEmpty(sdkVersion))
            {
                // Download SDK if it does not exist
                if (!File.Exists(CachePath.GetSDKFilePath(sdkVersion)))
                {
                    string url      = "https://github.com/TechCor8/OP2DotNetMissionSDK/releases/download/" + sdkVersion + "/" + CachePath.GetSDKFileName(sdkVersion);
                    string destPath = CachePath.GetSDKFilePath(sdkVersion);

                    using (UnityWebRequest request = UnityWebRequest.Get(url))
                    {
                        progressDialog.SetTitle("Downloading " + CachePath.GetSDKFileName(sdkVersion));
                        progressDialog.SetWebRequest(request);

                        yield return(request.SendWebRequest());

                        if (!DidDownloadSucceed(request, true))
                        {
                            progressDialog.Close();
                            OnClick_Delete();
                            yield break;
                        }

                        WriteFile(destPath, request.downloadHandler.data);
                    }
                }

                // Download SDK Interop if it does not exist or is from an older SDK
                if (!File.Exists(CachePath.GetInteropFilePath()) || CachePath.IsNewerVersion(sdkVersion, CacheDetails.interopVersion))
                {
                    string url      = "https://github.com/TechCor8/OP2DotNetMissionSDK/releases/download/" + sdkVersion + "/" + CachePath.DotNetInteropFileName;
                    string destPath = CachePath.GetInteropFilePath();

                    using (UnityWebRequest request = UnityWebRequest.Get(url))
                    {
                        progressDialog.SetTitle("Downloading " + CachePath.DotNetInteropFileName);
                        progressDialog.SetWebRequest(request);

                        yield return(request.SendWebRequest());

                        if (!DidDownloadSucceed(request, true))
                        {
                            progressDialog.Close();
                            OnClick_Delete();
                            yield break;
                        }

                        WriteFile(destPath, request.downloadHandler.data);
                    }
                }
            }

            progressDialog.Close();

            // Mission fully downloaded.
            // Write mission details
            string detailsJson = JsonUtility.ToJson(missionData);

            File.WriteAllText(CachePath.GetMissionDetailsFilePath(missionData.missionID), detailsJson);

            // Add mission details reference
            CacheDetails.AddMissionData(missionData);

            // Set buttons
            m_BtnDownload.gameObject.SetActive(false);
            m_BtnDelete.gameObject.SetActive(true);
            m_BtnInstall.interactable = true;
        }
Esempio n. 3
0
        public void OnClick_Install()
        {
            // Get the cached mission details
            string      json       = File.ReadAllText(CachePath.GetMissionDetailsFilePath(missionData.missionID));
            MissionData localData  = JsonUtility.FromJson <MissionData>(json);
            string      sdkVersion = CachePath.GetSDKVersion(localData);

            // Do not allow installation if mission files already exist
            foreach (string fileName in localData.fileNames)
            {
                string filePath = Path.Combine(UserPrefs.gameDirectory, fileName);

                if (File.Exists(filePath))
                {
                    InfoDialog.Create("Installation Failed", "The file '" + fileName + "' already exists in your game directory. Please remove it to continue.");
                    return;
                }
            }

            List <string> installedFiles = new List <string>();

            // Need to export plugin for standard mission OPM file
            if (m_IsStandardMission)
            {
                string opmFileName = localData.fileNames.FirstOrDefault((string fileName) => Path.GetExtension(fileName).ToLowerInvariant().Contains("opm"));
                string opmFilePath = Path.Combine(CachePath.GetMissionDirectory(localData.missionID), opmFileName);

                string pluginFileName = Path.ChangeExtension(opmFileName, ".dll");
                string pluginPath     = Path.Combine(UserPrefs.gameDirectory, pluginFileName);

                // Don't allow install if the plugin will overwrite another DLL of the same name
                if (File.Exists(pluginPath))
                {
                    InfoDialog.Create("Install Failed", "There is already a plugin named " + pluginFileName);
                    return;
                }

                // Export plugin
                MissionRoot root = MissionReader.GetMissionData(opmFilePath);
                PluginExporter.ExportPlugin(pluginPath, root.sdkVersion, root.levelDetails);

                FileReference.AddReference(pluginFileName);

                installedFiles.Add(pluginFileName);
            }

            // Install mission from cache into game folder
            foreach (string fileName in localData.fileNames)
            {
                string filePath = Path.Combine(CachePath.GetMissionDirectory(localData.missionID), fileName);

                InstallFile(fileName, filePath);

                installedFiles.Add(fileName);
            }

            // Install SDK
            if (!string.IsNullOrEmpty(sdkVersion))
            {
                InstallFile(CachePath.GetSDKFileName(sdkVersion), CachePath.GetSDKFilePath(sdkVersion));
                InstallFile(CachePath.DotNetInteropFileName, CachePath.GetInteropFilePath(), true);

                installedFiles.Add(CachePath.GetSDKFileName(sdkVersion));
                installedFiles.Add(CachePath.DotNetInteropFileName);
            }

            // Write installed files to cache
            using (FileStream fs = new FileStream(CachePath.GetMissionInstalledMetaFilePath(localData.missionID), FileMode.Create, FileAccess.Write, FileShare.Read))
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    foreach (string fileName in installedFiles)
                    {
                        writer.WriteLine(fileName);
                    }
                }

            // Set buttons
            m_BtnInstall.gameObject.SetActive(false);
            m_BtnUninstall.gameObject.SetActive(true);
            m_BtnDelete.interactable = false;
        }
Esempio n. 4
0
        public void Initialize(MissionData mission)
        {
            missionData = mission;

            m_InputTitle.text       = mission.missionName;
            m_InputDescription.text = mission.missionDescription;
            m_txtGitHub.text        = mission.gitHubLink;

            // DLL or OPM
            string dllName = mission.fileNames.FirstOrDefault((filename) => filename.IndexOf(".dll", System.StringComparison.OrdinalIgnoreCase) >= 0);
            string opmName = mission.fileNames.FirstOrDefault((filename) => filename.IndexOf(".opm", System.StringComparison.OrdinalIgnoreCase) >= 0);

            m_IsStandardMission = string.IsNullOrEmpty(dllName) && !string.IsNullOrEmpty(opmName);
            string mainFileName = m_IsStandardMission ? opmName : dllName;
            bool   isStub       = string.IsNullOrEmpty(mainFileName);

            // Mission type string
            string missionType = "Unknown";

            if (isStub)
            {
                missionType = "Stub";
            }
            else if (mainFileName.StartsWith("c"))
            {
                missionType = "Colony";
            }
            else if (mainFileName.StartsWith("a"))
            {
                missionType = "Auto Demo";
            }
            else if (mainFileName.StartsWith("t"))
            {
                missionType = "Tutorial";
            }
            else if (mainFileName.StartsWith("mu"))
            {
                missionType = "Multiplayer Land Rush";
            }
            else if (mainFileName.StartsWith("mf"))
            {
                missionType = "Multiplayer Space Race";
            }
            else if (mainFileName.StartsWith("mr"))
            {
                missionType = "Multiplayer Resource Race";
            }
            else if (mainFileName.StartsWith("mm"))
            {
                missionType = "Multiplayer Midas";
            }
            else if (mainFileName.StartsWith("ml"))
            {
                missionType = "Multiplayer Last One Standing";
            }

            // Certification image
            bool isCertified = !string.IsNullOrEmpty(mission.certifyingAdminName) || m_IsStandardMission || isStub;

            m_Trusted.SetActive(isCertified);
            m_Untrusted.SetActive(!isCertified);

            m_BtnCertify.GetComponentInChildren <Text>().text = string.IsNullOrEmpty(mission.certifyingAdminName) ? "Certify" : "Uncertify";

            // Certification string
            string certifiedString = mission.certifyingAdminName;

            if ((m_IsStandardMission || isStub) && string.IsNullOrEmpty(certifiedString))
            {
                certifiedString = "Not Required";
            }
            else if (!m_IsStandardMission && string.IsNullOrEmpty(certifiedString))
            {
                certifiedString = "<color=yellow>Not Certified</color>";
            }

            // Mission Details
            string details = m_IsStandardMission ? "Standard Mission" : "Custom Mission";

            details += "\nAuthor: " + mission.authorName;
            details += "\nCertified by: " + certifiedString;
            details += "\n";
            details += "\nType: " + missionType;
            //details += "\nPlayers: 6" + missionType;
            //details += "\nUnit Only: True" + missionType;
            details += "\n";
            details += "\nFiles:";
            details += "\n" + string.Join(", ", mission.fileNames);

            m_InputDetails.text = details;

            // We can upload files if the mission exists. This button won't be visible if the user does not have permission.
            m_BtnUploadFile.interactable = true;
            m_BtnDeleteFile.interactable = missionData.fileNames.Count > 0;

            // Read local mission version
            if (File.Exists(CachePath.GetMissionDetailsFilePath(missionData.missionID)))
            {
                string      json      = File.ReadAllText(CachePath.GetMissionDetailsFilePath(missionData.missionID));
                MissionData localData = JsonUtility.FromJson <MissionData>(json);
                m_LocalVersion = localData.version;
            }
            else
            {
                // No local version exists, so use the current version
                m_LocalVersion = missionData.version;
            }

            // Show "New version available" text
            if (m_LocalVersion < missionData.version)
            {
                m_txtDifferentVersion.text = "New version available.\nDelete to redownload.";
            }
            else if (m_LocalVersion > missionData.version)
            {
                m_txtDifferentVersion.text = "Older version available.\nDelete to redownload.";
            }
            else
            {
                m_txtDifferentVersion.text = "";
            }

            // Show proper download state
            bool isCached = File.Exists(CachePath.GetMissionDetailsFilePath(missionData.missionID));

            m_BtnDownload.gameObject.SetActive(!isCached);
            m_BtnDelete.gameObject.SetActive(isCached);
            m_BtnDownload.interactable = true;

            // Show proper install state
            bool isInstalled = File.Exists(CachePath.GetMissionInstalledMetaFilePath(missionData.missionID));

            m_BtnInstall.gameObject.SetActive(!isInstalled);
            m_BtnUninstall.gameObject.SetActive(isInstalled);
            m_BtnInstall.interactable   = isCached;             // You can only install if the mission is cached
            m_BtnUninstall.interactable = true;                 // If installed, you can always uninstall. This button is only visible in that case.
            m_BtnDelete.interactable    = !isInstalled;
        }