public AssignedBehavior AddBehavior(UnassignedBehavior behavior)
    {
        OnBeforeAnyChange();
        Debug.Assert(!behavior.behaviorUri.IsNullOrEmpty());

        string finalBehaviorUri = behavior.behaviorUri;

        // Alright, some special logic here. If it's a user-library behavior, do not
        // just use the URI directly. Import it, so turn it into embedded, then use
        // it. We want VOOS files to be stand-alone, so we can't have any local user
        // library dependencies.
        if (BehaviorSystem.IsUserLibraryBehaviorUri(behavior.behaviorUri))
        {
            Behaviors.Behavior importedBehavior = GetBehaviorSystem().GetBehaviorData(behavior.behaviorUri);
            Debug.Assert(!importedBehavior.userLibraryFile.IsNullOrEmpty());
            string importedId = GetBehaviorSystem().GenerateUniqueId();
            GetBehaviorSystem().PutBehavior(importedId, importedBehavior);
            string importedUri = BehaviorSystem.IdToEmbeddedBehaviorUri(importedId);
            finalBehaviorUri = importedUri;
        }

        // Create the use in the database
        string useId = actor.GetBehaviorSystem().GenerateUniqueId();
        var    brain = GetBrain();

        brain.AddUse(new BehaviorUse
        {
            id                  = useId,
            behaviorUri         = finalBehaviorUri,
            propertyAssignments = new Behaviors.PropertyAssignment[0]
        });
        GetBehaviorSystem().PutBrain(GetBrainId(), brain);

        return(new AssignedBehavior(useId, this));
    }
Exemplo n.º 2
0
    // Maybe cached.
    Behaviors.Behavior CachedLoadBehavior(string absPath, string label)
    {
        if (loadedBehaviorsByAbsPath.ContainsKey(absPath))
        {
            return(loadedBehaviorsByAbsPath[absPath]);
        }
        else
        {
            string js       = File.ReadAllText(absPath);
            string metaJson = null;

            string metaPath = absPath + ".metaJson";
            if (File.Exists(metaPath))
            {
                metaJson = File.ReadAllText(metaPath);
            }

            Behaviors.Behavior beh = new Behaviors.Behavior
            {
                label        = label,
                javascript   = js,
                metadataJson = metaJson
            };
            loadedBehaviorsByAbsPath.Add(absPath, beh);
            return(beh);
        }
    }
Exemplo n.º 3
0
 public void SetMetadataJson(string metadataJson)
 {
     Debug.Assert(IsValid(), "Function called on invalid UnassignedBehavior");
     Behaviors.Behavior behavior = GetBehavior();
     behavior.metadataJson = metadataJson;
     behaviorSystem.PutBehavior(BehaviorSystem.GetIdOfBehaviorUri(behaviorUri), behavior);
 }
Exemplo n.º 4
0
    public int WriteEmbeddedBehaviorsToDirectory(IEnumerable <string> behaviorUris, string path)
    {
        int count = 0;

        foreach (string uri in behaviorUris)
        {
            string             id                = BehaviorSystem.GetIdOfBehaviorUri(uri);
            Behaviors.Behavior behavior          = GetBehaviorData(uri);
            BehaviorCards.CardMetadata.Data meta = BehaviorCards.CardMetadata.GetMetaDataFor(behavior);
            string fileName = String.Join("_",
                                          meta.title.Split(Path.GetInvalidFileNameChars(), StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
            string baseBehaviorDir = Path.Combine(path, fileName);
            string behaviorDir     = baseBehaviorDir;
            int    i = 1;
            while (Directory.Exists(behaviorDir))
            {
                behaviorDir = baseBehaviorDir + " " + i;
                i++;
            }
            Directory.CreateDirectory(behaviorDir);
            Behaviors.Behavior.WriteToDirectory(behaviorDir, id, behavior);
            count++;
        }
        return(count);
    }
Exemplo n.º 5
0
    public UnassignedBehavior MakeCopy()
    {
        Behaviors.Behavior behavior   = GetBehavior();
        string             behaviorId = behaviorSystem.GenerateUniqueId();

        Debug.Log(behavior.label + ", " + behavior.metadataJson + ", " + behavior.javascript);
        behaviorSystem.PutBehavior(behaviorId, new Behaviors.Behavior
        {
            label        = behavior.label,
            metadataJson = behavior.metadataJson,
            javascript   = behavior.javascript
        });
        return(new UnassignedBehavior(BehaviorSystem.IdToEmbeddedBehaviorUri(behaviorId), behaviorSystem));
    }
Exemplo n.º 6
0
    public void SetDraftCode(string s)
    {
        Debug.Assert(IsValid(), "Function called on invalid UnassignedBehavior");
        Behaviors.Behavior behavior = GetBehavior();

        if (behavior.draftJavascript == s)
        {
            Debug.Log("No actual change to draft behavior code. Doing nothing.");
            return;
        }

        behavior.draftJavascript = s;
        behaviorSystem.PutBehavior(BehaviorSystem.GetIdOfBehaviorUri(behaviorUri), behavior);
    }
Exemplo n.º 7
0
    public void PutBehavior(string behaviorId, Behaviors.Behavior behavior)
    {
        Debug.Assert(!IsBehaviorUri(behaviorId), "Please provide an id, not an uri");

        // Follow predicted-last-writer-wins pattern.
        var req = new PutRequest <Behaviors.Behavior> {
            id = behaviorId, value = behavior
        };
        string json = JsonUtility.ToJson(req);

        // Make sure we put in the same deserialized version as the RPC would
        // (to avoid whitespace differences && make Behavior.Equals() comparisons consistent)
        req = JsonUtility.FromJson <PutRequest <Behavior> >(json);
        PutBehaviorLocal(req, true);
        photonView.RPC("PutBehaviorRPC", PhotonTargets.AllViaServer, json);
    }
Exemplo n.º 8
0
 public void ClearAllButThis(Behavior a)
 {
     // do we want to Detach all current entities, calling their AliveChanged?
     this.Clear();
     behaviorList.AddLast(a);
 }
Exemplo n.º 9
0
 public void Attach(Behavior behavior)
 {
     behaviorList.AddFirst(behavior);
     behavior.Attached     = true;
     behavior.ParentHolder = this;
 }
Exemplo n.º 10
0
    public void Setup()
    {
        Util.FindIfNotSet(this, ref behaviorSystem);
        Util.FindIfNotSet(this, ref claimKeeper);
        Util.FindIfNotSet(this, ref undoStack);
        Util.FindIfNotSet(this, ref voosEngine);
        Util.FindIfNotSet(this, ref popups);
        newCardUI.newCardButton.onClick.AddListener(() =>
        {
            string category = newCardCategoryOptions[newCardUI.newCardCategoryDropdown.value].text;
            AddNewCard(category);
        });
        cardLibraryUI.closeButton.onClick.AddListener(Close);
        cardLibraryUI.inputField.onValueChanged.AddListener(OnInputFieldChanged);
        cardLibraryUI.clearSearchButton.onClick.AddListener(ClearSearch);
        cardLibraryUI.categoryDropdown.onValueChanged.AddListener((v) =>
        {
            MatchNewCardCategoryToFilter();
            UpdateCards();
        });
        cardDetail = Instantiate(cardDetailPrefab);
        cardDetail.Setup(parentRect);

        cardDetail.onCodeCard += (model, _, container) =>
        {
            if (!model.IsValid())
            {
                return;
            }
            if (model.IsBuiltin())
            {
                // Not embedded, need to copy first
                string newBehaviorUri = model.MakeCopy().GetUri();
                onCodeRequest?.Invoke(newBehaviorUri);
            }
            else
            {
                onCodeRequest?.Invoke(model.GetUri());
            }
        };
        cardDetail.onPreviewCard += (model) =>
        {
            onCodeRequest?.Invoke(model.GetUri());
        };
        cardDetail.onTrashCard += (model, _) =>
        {
            if (!model.IsValid())
            {
                return;
            }

            ClaimableUndoUtil.ClaimableUndoItem undoItem = new ClaimableUndoUtil.ClaimableUndoItem();
            Behaviors.Behavior behavior = model.GetUnassignedBehaviorItem().GetBehavior();
            string             uri      = model.GetUri();
            string             id       = model.GetId();
            undoItem.resourceId   = UnassignedBehavior.GetClaimResourceId(id);
            undoItem.resourceName = model.GetTitle();
            undoItem.label        = $"Delete {model.GetTitle()}";
            undoItem.doIt         = () =>
            {
                behaviorSystem.DeleteBehavior(id);
            };
            undoItem.undo = () =>
            {
                behaviorSystem.PutBehavior(id, behavior);
            };
            undoItem.cannotDoReason = () =>
            {
                if (voosEngine.FindOneActorUsing(uri) != null)
                {
                    return("One or more actors are using this card.");
                }
                return(null);
            };
            ClaimableUndoUtil.PushUndoForResource(undoStack, claimKeeper, undoItem);
        };

        behaviorSystem.onBehaviorPut    += OnBehaviorPut;
        behaviorSystem.onBehaviorDelete += OnBehaviorDelete;

#if USE_STEAMWORKS
        uploadDialog = Instantiate(uploadDialogPrefab);
        uploadDialog.Setup();

        workshopMenu = Instantiate(workshopMenuPrefab);
        workshopMenu.Setup();
#endif

        cardPackPicker = Instantiate(cardPackPickerPrefab);

        cardLibraryUI.selectionDoneButton.onClick.AddListener(() =>
        {
            Debug.Assert(selection != null);
            selection.selectionFinishedCallback(selection.selectedCards);
            EndSelection();
        });

        cardLibraryUI.selectionCancelButton.onClick.AddListener(() =>
        {
            EndSelection();
        });

#if USE_STEAMWORKS
        cardLibraryUI.exportDropdown.SetOptions(new List <string>()
        {
            EXPORT_TO_DISK, EXPORT_TO_WORKSHOP, UPDATE_WORKSHOP_PACK
        });
#else
        // cardLibraryUI.exportDropdown.SetOptions(new List<string>() {
        //   EXPORT_TO_DISK
        // });
        cardLibraryUI.exportButton.onClick.AddListener(() => StartSelection(OnFinishSelectionForLocal));
#endif

        cardLibraryUI.exportDropdown.onOptionClicked += (value) =>
        {
            if (value == EXPORT_TO_DISK)
            {
                StartSelection(OnFinishSelectionForLocal);
            }
#if USE_STEAMWORKS
            else if (value == UPDATE_WORKSHOP_PACK)
            {
                cardPackPicker.Open((res) =>
                {
                    if (res.IsEmpty())
                    {
                        return;
                    }
                    StartSelection((list) => OnFinishSelectionForWorkshop(list, res), res);
                });
            }
            else
            {
                StartSelection(OnFinishSelectionForWorkshop, Util.Maybe <ulong> .CreateEmpty());
            }
#endif
        };

#if USE_STEAMWORKS
        cardLibraryUI.importDropdown.SetOptions(new List <string>()
        {
            IMPORT_FROM_DISK, IMPORT_FROM_WORKSHOP
        });
#else
        cardLibraryUI.importButton.onClick.AddListener(ImportLocal);

        // cardLibraryUI.importDropdown.SetOptions(new List<string>() {
        //   IMPORT_FROM_DISK
        // });
#endif

        cardLibraryUI.importDropdown.onOptionClicked += (value) =>
        {
            if (value == IMPORT_FROM_DISK)
            {
                ImportLocal();
            }
            else
            {
                workshopMenu.Open();
            }
        };

        PopulateCards();
    }